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

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

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

✓ Accepted answer

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 :)

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