I’m trying to make a script that detects when a new player has joined the game and immediately starts giving them money every second. This is what I have so far:
game.Players.PlayerAdded:Connect(function(plr)
local cash = plr.leaderstats.Cash.Value
while true do
cash = cash + 1
wait (1)
end
end)
However, when the game starts this error is produced:
Are you sure you create the leaderstats before the code is run? Try add this code at the bottom of where the leaderstats get created or use the :WaitForChild() to wait for the leaderstats to be made.
The user stated in the main post saying that “leaderstats is working fine and shows perfectly” which means this cannot be the issue because they clearly know that it has been created. No need to explain what leaderstats are.
It’s possible this code is running before leaderstats has been even created then. Just make a check to ensure it actually exists using :WaitForChild().
Sorry, just not used to this way of coding relying on while true do loops requiring you to break code up code, was just assuming leaderstats would have been all managed in a single script.
game.Players.PlayerAdded:Connect(function(plr)
local cash = plr:WaitForChild("leaderstats", math.huge):WaitForChild("Cash", math.huge)
while true do
cash.Value = cash.Value + 1
wait (1)
end
end)