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

coin giver script sometimes working? other times an error appears in the output

so i'm making an obby game. when a player touches the end they get 10 coins. or that's what supposed to happen. when the player touches the end sometimes they get 10 coins and other times nothing happenes, and an error appears in the output. the error is something like "attempt to index nil with leaderstats" here's the script.

script.Parent.Touched:Connect(function(plr)
game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Coins.Value = game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Coins.Value + 10
end)

thanks!

1 answer

✓ Accepted answer

It's better to do this:

script.Parent.Touched:Connect(function(plr)
local leaderstats = game.Players:GetPlayerFromCharacter(plr.Parent):WaitForChild("leaderstats")
leaderstats.Coins.Value = leaderstats.Coins.Value + 10
end)

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