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

Why won't my GamePass script work?

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)

1 comment

0

PlayerHasPass doesn't work in a local script, Make it a Regular Script and do PlayerAdded instead of child added.

1 answer

✓ 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

0

Thanks so much!

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