✓ Accepted answer
Code Fixes
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.
Efficiency Fixes
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.
Code
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
1 comment
PlayerHasPass doesn't work in a local script, Make it a Regular Script and do PlayerAdded instead of child added.