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

[SOLVED] Why is game not giving player cash every 5 seconds? (probably easy)

Asked by 3 years ago
Edited 3 years ago

So I'm making a roleplay type game and I want it to give cash every 5 seconds but it isn't working. This is a script in the ServerScriptService. Here is the code

local plr = game.Players.LocalPlayer

while true do

    plr.leaderstats.Cash.value = plr.leaderstats.Cash.value + 5
    wait(5)
end

If you know what is wrong send me the new code. leaderstats is a model not folder. The error is 16:37:15.276 - ServerScriptService.Script:5: attempt to index nil with 'leaderstats' Thanks!

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You're trying to access the LocalPlayer through a ServerScript, this will return nil thus there is nothing to access from it, so what you should do is:

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = player:WaitForChild('leaderstats')

    while wait(5) do
        leaderstats.Cash.Value = leaderstats.Cash.Value + 5
    end
end)

Hopefully, this helps :)

Also, it seems like you are still learning the basics of scripting, so I suggest researching about LocalScripts and Scripts :)

Ad

Answer this question