Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why won't my GamePass script work?

Asked by 9 years ago

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)
0
PlayerHasPass doesn't work in a local script, Make it a Regular Script and do PlayerAdded instead of child added. EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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

0
Thanks so much! attackonkyojin 135 — 9y
Ad

Answer this question