Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with my gamepass script? [SOLVED]

I'm honestly clueless on what to do. Basically I have a gamepass and when the user purchases it they have something they can do. In the end, I want the script to run and when they join the game if the player has the gamepass I want it to turn a textbutton visibility to true (Does not matter what the name of the gui is). I checked the wiki but there example just seemed to confusing for me. Maybe if someone could explain it a little more and show me how to do it I might get a better understanding. Thanks!

local GamePassService = game:GetService("GamePassService")
local GamePassId = 185260715

game.Players.PlayerAdded:connect(function(player)
	if GamePassService:PlayerHasPass(player, GamePassId) then
		print("Username: " ..player " got dat pass")
		game.Players[player].PlayerGui.MenuGUI.Menu.twoDoff.Visible = true
	end
end)

1 answer

✓ Accepted answer

Your error is line 7.

game.Players[player].PlayerGui.MenuGUI.Menu.twoDoff.Visible = true

The player parameter is equal to the player object that joins the game. It is not a string. You could technically do,

game.Players[player.Name]

but this is completely unnecessary and pointless! player is the player object, so you can use it directly!

player.PlayerGui.MenuGUI.Menu.twoDoff.Visible = true

In some situations, like when the player is lagging, you may want to delay the PlayerAdded event until their character is loaded, to prevent errors.

repeat wait() until player.Character

or, when using PlayerGui, delay the script until the PlayerGui is loaded.

player:WaitForChild("PlayerGui")

5 comments

0

This is a script, should it be a localscript?

0

Whoops I see now, thanks lol my mistake.

0

It should be a server Script in Workspace or ServerScriptService. PlayerAdded events don't work well in LocalScripts, but we could edit the code to work correctly in LocalScripts if needed.

0

So what example would you use? The last one you wrote Perci? And also what line would you insert it into?

0

WaitForChild() delays the script until the child exists, then returns that child. You can therefore use it directly, e.g. 'player:WaitForChild("PlayerGui").Gui:Destroy()' . I use WaitForChild() a lot when scripting for it helps prevent errors due to lag.

This discussion is preserved as it was. New answers, comments, and votes are closed.