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

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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

Answer this question