Button that only appears when you have a gamepass

Hello, I wish for there to be a button that ONLY appears if they do not have a certain gamepass. When they click the button, it will prompt them to buy the gamepass.

I have tried to check using the function PlayerHasPass but that doesn’t work.

All help will be appreciated. :smiley:

1 Like

First I would recommend defining the Gamepass’ ID in a Variable like so;

local ProductID = “Your ProductID”

This way it’s a lot easier to access in future reference.

You would need to check if the player has clicked the button first;

(This is deprecated so I recommend using UserInputService instead of this)

script.Parent.MouseButton1Click:Connect(function))

end)

And finally it should all look like this;

local MarketplaceService = game:GetService(“MarketplaceService”)
local Player = game.Players.LocalPlayer

local ProductID = …

script.Parent.MouseButton1Click:Connect(function))

local success, message = pcall(function()

    haspass = MarketplaceService:UserOwnsGamepassAsync(Player.UserID, ProductID)

end)

if haspass then

    print(“Player already owns pass”) — Do not prompt purchase

else

    MarketplaceService:PromptGamePassPuchase(PPlayer, ProductID)

end

End)

1 Like

Sorry it looks like some of the code didn’t show as needed, it should all work. Although you would need every Variable, the code begins from the Variable “local MarketplaceService” everything below that is needed

This script didn’t work, what I put was:

local MarketplaceService = game:GetService("MarketplaceService")
local Player = game.Players.LocalPlayer

local ProductID = 11067763

script.Parent.MouseButton1Click:Connect(function()
local success, message = pcall(function()

    haspass = MarketplaceService:UserOwnsGamepassAsync(Player.UserID, ProductID)

end)

if haspass then

    script.Parent.Visible=false

else

    MarketplaceService:PromptGamePassPuchase(Player, ProductID)

end

When I click the button, it doesn’t prompt me to buy the gamepass.

The issue there is that you didn’t you the variables that didn’t show, place the following above

local Player = game.Player.LocalPlayer

Here is what you need to add;

local MarketplaceService = game:GetService(“MarketplaceService”)

This will prompt the game to get the MarketplaceService which is used for things like this.

Both of those are already in the code…

I see, are there any output warnings? That might help resolve this issue

Players.BlockyToby.PlayerGui.UI.UI.ImageButton.LocalScript:21: Expected 'end' (to close 'function' at line 6), got <eof>; did you forget to close 'function' at line 7?

I will close the function and see if it works then.

1 Like

I fixed the issue, and the prompt still doesn’t work. :confused:

Thats strange, can you provide an image of the script to ensure that the forum doesn’t cut any of it out? The script works fine for me so perhaps its another issue.

I fixed it :+1:
What I had to do was change the script to a local script.