Text appears even when player doesn't purchase dev product

i am making a game advertisement system working with gui but the ad text shows up when the player opens the product before getting purchased
script:

local box = script.Parent
local gameImg = box.Parent:WaitForChild("GameImage")
local advertisebtn = box.Parent:WaitForChild("Advertise")
local mps = game:GetService("MarketplaceService")
local adtext = box.Parent.Parent:WaitForChild("AdvertisementText")
local plr = game.Players.LocalPlayer

productID = 1878843532

box.FocusLost:Connect(function(EP, ifl)
	if EP then
		local id = tonumber(box.Text)
		if id then
			local gameInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
			if gameInfo then
				gameImg.Image = "rbxassetid://" .. gameInfo.IconImageAssetId
				box.Text = "Game Detected !"
				wait(1)
				box.Text = ""
				wait(0.1)
				advertisebtn.MouseButton1Click:Connect(function()
					local gameId = tonumber(box.Text)
					mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
					adtext.Visible = true
					adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
					wait(4)
					adtext.Visible = false
				end)
			end
		end
	end
end)
3 Likes

mps:PromptProductPurchase() doesn’t cancel the script if the purchase fails, nor does it pause the script while waiting for a player response.

Consider reviewing the documentation on how to learn if a player purchased the product (it should take you to the handling purchases documentation).
Developer Products | Documentation - Roblox Creator Hub

If your goal is to show the ad after they close the purchase window if they chose not to buy the product, then review this documentation (it should take you to the PromptProductPurchaseFinished event documentation):
MarketplaceService | Documentation - Roblox Creator Hub

2 Likes

do we use if statement with it?

1 Like

Can you be more specific about what you are referring to?

1 Like

like if mps:PromptProductPurchase() then
end)

1 Like

No, mps:PromptProductPurchase() connects to an internal ROBLOX function, and wouldn’t return anything. I think your current script is fine. You just need to add an event such as:

mps:PromptProductPurchase(game.Players.LocalPlayer, productID) --from your provided script
local finished = false
local connection
connection = mps:PromptProductPurchaseFinished:Connect(function(UserID)
   if UserID = plr.UserId then
      finished = true
   end
end)

while finished = false do wait() end

--rest of your script goes here as normal

This would go after mps:PromptProductPurchase(game.Players.LocalPlayer, productID), make sure you don’t accidentally double it when putting this in the script.

2 Likes

Has your problem been solved? If so, please mark the response that I gave as a solution. Otherwise, inform me of what discrepancies still exist.

i am sorry i am getting in studio i took a small break

1 Like

didn’t work here is the new script:

local box = script.Parent
local gameImg = box.Parent:WaitForChild("GameImage")
local advertisebtn = box.Parent:WaitForChild("Advertise")
local mps = game:GetService("MarketplaceService")
local adtext = box.Parent.Parent:WaitForChild("AdvertisementText")
local plr = game.Players.LocalPlayer

productID = 1878843532

box.FocusLost:Connect(function(EP, ifl)
	if EP then
		local id = tonumber(box.Text)
		if id then
			local gameInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
			if gameInfo then
				gameImg.Image = "rbxassetid://" .. gameInfo.IconImageAssetId
				box.Text = "Game Detected !"
				wait(1)
				box.Text = ""
				wait(0.1)
				advertisebtn.MouseButton1Click:Connect(function()
					local gameId = tonumber(box.Text)
					local purchased = false
					mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
					purchased = true
					if purchased == true then
						game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
						adtext.Visible = true
						adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
						wait(4)
						adtext.Visible = false
					end
				end)
			end
		end
	end
end)
1 Like

Unfortunately, you forgot to implent this important part of the script:

local connection
connection = mps:PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
   if UserID = plr.UserId then
      finished = true
   end
end)

while finished = false do wait() end
connection:Disconnect()

It should go after mps:PromptProductPurchase(game.Players.LocalPlayer, productID)

doesn’t work either
script:


local box = script.Parent
local gameImg = box.Parent:WaitForChild("GameImage")
local advertisebtn = box.Parent:WaitForChild("Advertise")
local mps = game:GetService("MarketplaceService")
local adtext = box.Parent.Parent:WaitForChild("AdvertisementText")
local plr = game.Players.LocalPlayer

productID = 1878843532

box.FocusLost:Connect(function(EP, ifl)
	if EP then
		local id = tonumber(box.Text)
		if id then
			local gameInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
			if gameInfo then
				gameImg.Image = "rbxassetid://" .. gameInfo.IconImageAssetId
				box.Text = "Game Detected !"
				wait(1)
				box.Text = ""
				wait(0.1)
				advertisebtn.MouseButton1Click:Connect(function()
					local gameId = tonumber(box.Text)
					local purchased = false
					mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
						game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
						adtext.Visible = true
						adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
						wait(4)
						adtext.Visible = false
					local connection
					connection = mps:PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
						if UserID = plr.UserId then
							finished = true
						end
					end)

					while finished = false do wait() end
					connection:Disconnect()
				end)
			end
		end
	end
end)
1 Like

I can see that you don’t quite understand where it fits into the code, please inform me if the following works, I am only showing the part of the code in the advertisebtn.MouseButton1Click connection.

advertisebtn.MouseButton1Click:Connect(function()
	local gameId = tonumber(box.Text)
	local purchased = false
	mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
	local connection
	connection = mps:PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
		if UserID = plr.UserId then
			finished = true
		end
	end)
	while finished = false do wait() end
	connection:Disconnect()
	game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
	adtext.Visible = true
	adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
	wait(4)
	adtext.Visible = false
end)

I hope this works.
Essentially, the logic of the code is, waiting for the purchase window to be removed by the player, and then running the advertisement code.

1 Like

sadly it doesn’t work fully
11111111111111

what is the error?

Additionally, I have considered that you may only want the code to run when the player actually purchases the item, rather than just opening the prompt and closing it, I can provide assistance in managing that if so.

i have fixed every error now it does the same thing that occured

script:

local box = script.Parent
local gameImg = box.Parent:WaitForChild("GameImage")
local advertisebtn = box.Parent:WaitForChild("Advertise")
local mps = game:GetService("MarketplaceService")
local adtext = box.Parent.Parent:WaitForChild("AdvertisementText")
local plr = game.Players.LocalPlayer

productID = 1878843532

box.FocusLost:Connect(function(EP, ifl)
	if EP then
		local id = tonumber(box.Text)
		if id then
			local gameInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
			if gameInfo then
				gameImg.Image = "rbxassetid://" .. gameInfo.IconImageAssetId
				box.Text = "Game Detected !"
				wait(1)
				box.Text = ""
				wait(0.1)
				advertisebtn.MouseButton1Click:Connect(function()
					local gameId = tonumber(box.Text)
					local purchased = false
					mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
					local connection
					connection = mps.PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
						if UserID == plr.UserId then
							finished = true
						end
					end)
					while finished == false do wait() end
					connection:Disconnect()
					game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
					adtext.Visible = true
					adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
					wait(4)
					adtext.Visible = false
				end)
				end
			end
		end
end)

I believe I made a small mistake in the naming of my variables. Here is the correction:

advertisebtn.MouseButton1Click:Connect(function()
	local gameId = tonumber(box.Text)
	local finished = false
	mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
	local connection
	connection = mps:PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
		if UserID = plr.UserId then
			finished = true
		end
	end)
	while finished == false do wait() end
	connection:Disconnect()
	game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
	adtext.Visible = true
	adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
	wait(4)
	adtext.Visible = false
end)

apparently you are worng, i have fixed the script and it should be:

local box = script.Parent
local gameImg = box.Parent:WaitForChild("GameImage")
local advertisebtn = box.Parent:WaitForChild("Advertise")
local mps = game:GetService("MarketplaceService")
local adtext = box.Parent.Parent:WaitForChild("AdvertisementText")
local plr = game.Players.LocalPlayer

productID = 1878843532

box.FocusLost:Connect(function(EP, ifl)
	if EP then
		local id = tonumber(box.Text)
		if id then
			local gameInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
			if gameInfo then
				gameImg.Image = "rbxassetid://" .. gameInfo.IconImageAssetId
				box.Text = "Game Detected !"
				wait(1)
				box.Text = ""
				wait(0.1)
				advertisebtn.MouseButton1Click:Connect(function()
					local gameId = tonumber(box.Text)
					local finished = false
					mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
					local connection
					connection = mps.PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
						if UserID == plr.UserId then
							finished = true
							game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
							adtext.Visible = true
							adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
							wait(4)
							adtext.Visible = false
						end
					end)
					while finished == false do wait() end
connection:Disconnect()
				end)
				end
				end
	end
	end)

the idea you had was right but i have a small advice: in if statements you need to put “==” instead of just 1 “=” to prevent errors, same with while statement but thank you
another error u did is: you put mps:PromptProductFinished instead of putting “.” in the “mps:PromptProductFinished” you put “:”

1 Like

How silly that one little dot messed up the script. :slight_smile:

I did make some small, yet necessary changes. With your choice on the logic, you can remove all the finished variable stuff, and move the connection:Disconnect(), additionally the while statement can be removed.

local box = script.Parent
local gameImg = box.Parent:WaitForChild("GameImage")
local advertisebtn = box.Parent:WaitForChild("Advertise")
local mps = game:GetService("MarketplaceService")
local adtext = box.Parent.Parent:WaitForChild("AdvertisementText")
local plr = game.Players.LocalPlayer

productID = 1878843532

box.FocusLost:Connect(function(EP, ifl)
	if EP then
		local id = tonumber(box.Text)
		if id then
			local gameInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
			if gameInfo then
				gameImg.Image = "rbxassetid://" .. gameInfo.IconImageAssetId
				box.Text = "Game Detected !"
				wait(1)
				box.Text = ""
				wait(0.1)
				advertisebtn.MouseButton1Click:Connect(function()
					local gameId = tonumber(box.Text)
					mps:PromptProductPurchase(game.Players.LocalPlayer, productID)
					local connection
					connection = mps.PromptProductPurchaseFinished:Connect(function(UserID) --different from PromptProductPurchase
						if UserID == plr.UserId then
							connection:Disconnect()
							game.ReplicatedStorage.AdvertiseRE:FireServer(gameId)
							adtext.Visible = true
							adtext.Text = "Join"..tostring(gameInfo.Name) .."Made By  ".. plr.Name
							wait(4)
							adtext.Visible = false
						end
					end)
				end)
			end
		end
	end
end)

Please consider marking this as a solution.

And thank you for pointing out the equal sign error, I did manage to correct it a little bit back, but good work nevertheless finding it yourself.

1 Like

thank you because i have tested it and found some errors and decided to show it to you and thank you tho :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.