Buttons hidden if player doesn't own gamepass

Hello, any help anyone can give would be greatly appreciated :slight_smile:

I have got a couple of gamepasses that ‘unlock’ teleports when purchased, however the script didn’t make them visible (if invisible) and invisible if visible.

My script currently

local id = 9181149

game.Players.PlayerAdded:connect(function(player)
    if game:GetService("GamePassService"):PlayerHasPass(player, id) then
        script.Parent.Visible = true
else
    script.Parent.Visible = false
    end
end)

The script is inside the button and they are set to invisible by default currently in a bid to try and resolve the issue, but i had no such luck. Like i said, any help would be greatly appreciated. Thanks!

1 Like

Try using MarketplaceService instead of GamePassService.

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then
    script.Parent.Visible = true
else
    script.Parent.Visible = false
end
4 Likes

GamePassService is used for legacy Roblox games. It is always a good idea to check the Roblox API reference since sometimes certain coding methods have newer replacements.

Ok, im just loading onto my game to give it a try :slight_smile:

What kind of script is this? If it’s a LocalScript, there’s no reason to use the PlayerAdded event since you can just check the LocalPlayer. If it’s a server Script, you shouldn’t be manipulating UI at all with it.

2 Likes

It is a local script yes, located inside the buttons

Like I said, you shouldn’t be using the PlayerAdded event in a LocalScript. The player will have already joined by the time the localscript starts running. Just define player as the LocalPlayer, or game:GetService("Players").LocalPlayer.

2 Likes

So where would that go in relation to the whole script and which bits would it replace?

Instead of having the whole .PlayerAdded:Connect... bit, just do local player = game:GetService("Players").LocalPlayer. The full script would look something like this:

local player = game:GetService("Players").LocalPlayer
script.Parent.Visible = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id)
5 Likes

So the script is as simple as this?

Run it and see, that should work fine.

2 Likes

Ok, i shall do that now! Ill let you know what happens. Also, does it matter if the button is enabled/disabled by default before the script changes it?

Thank you! This appears to be working, very helpful. I appreciate the help you’ve all given!