Hello! I am currently trying to make a racing game and I want the player to be able to collect coins along the track. However, sometimes the coins won’t respawn and I will get this error:
Workspace.Coins.Respawning Coin.CollectionScript:8: attempt to index nil with ‘WaitForChild’
Here is my code on a server script:
local debounce = true
script.Parent.Hitbox.Touched:Connect(function(hit)
if debounce == true then
debounce = false
script.Parent.Transparency = 1
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local leaderstats = Player:WaitForChild("leaderstats")
local Coins = leaderstats:WaitForChild("Coins")
Coins.Value = Coins.Value + 1
script.Parent.CollectionSound:Play()
script.Parent.Transparency = 1
task.wait(1)
debounce = true
script.Parent.Transparency = 0
end
end)
If you think you know what went wrong, please let me know. Have a wonderful day!