So I want to make a button that detects when a player has bought a gamepass and things happen if they have. I looked around a lot but nothing seemed to help.
local mps = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local button = script.Parent
local hasPass = mps:UserOwnsGamePassAsync(player.UserId, 13411918)
script.Parent.MouseButton1Click:Connect(function()
mps:PromptGamePassPurchase(player, 13411918)
end)
game.Players.PlayerAdded:Connect(function()
if hasPass then
print("you have pass")
script.Parent.Text = "Bought"
end
end)
PlayerAdded will only fire for the 2nd player who joined after you. Meaning, if you joined the server, it will not fire. But if another player joined, it will fire.
This case only happens on local script, not server script.
If you want it to change the text of the GUI once, then remove the PlayerAdded event.
So it would be:
local mps = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local button = script.Parent
local hasPass = mps:UserOwnsGamePassAsync(player.UserId, 13411918)
script.Parent.MouseButton1Click:Connect(function()
mps:PromptGamePassPurchase(player, 13411918)
end)
if hasPass then
print("you have pass")
script.Parent.Text = "Bought"
end