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)
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
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.
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)
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)
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)
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.
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 “:”
How silly that one little dot messed up the script.
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.