I am trying to have it so that if the player has a gamepass, their tries go up by 1 every 5 seconds. Oh and the script is located in StarterPack as a LocalScript.
local plyr = game.Players.LocalPlayer local GamePassId = 253144150 game.Players.ChildAdded:connect(function(newPlayer) if (game:GetService("GamePassService"):PlayerHasPass(newPlayer,GamePassId)) then while true do wait(5) plyr.bin.LastLevel.Value = plyr.bin.Level.Value + 1 end end end)
The ChildAdded
event fires faster than the PlayerAdded
event, so when you're checking if they have the GamePass on line 4
then 'newPlayer' may be nil.
You should use a PlayerAdded event.
You should use the PlayerOwnsAsset
function of MarketplaceService, rather than the PlayerHasPass
function. PlayerOwnsAsset returns live results from the website while PlayerHasPass returns cached results from when the player entered the server. Meaning if a player buys the GamePass during gameplay then they won't have to rejoin with the PlayerOwnsAsset function.
But to make this take effect then you should also use a CharacterAdded
Event.
local ms = game:GetService('MarketplaceService') local pass = 253144150 game.Players.ChildAdded:connect(function(plr) local activated = false plr.CharacterAdded:connect(function(char) if ms:PlayerOwnsAsset(plr,pass) then activated = true while activated do wait(5) local stat = plr.bin.:FindFirstChild('LastLevel') if stat then stat.Value = stat.Value + 1 end end end end) end)
NOTE: This should be a server-script in workspace