How to detect when player owns gamepass?

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)

Do you want to detect if the player bought a gamepass or if the player owns a gamepass?

mostly owns a gamepass but maybe both

For detecting if the player owns a gamepass:

For detecting when a gamepass has been bought:

I used both of those but none of them seemed to work, look at the script. I deleted the promptgamepasspurchasefinished part though.

Those events should work if the script you did is properly coded.

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.

1 Like

So then what should I add to the 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
1 Like