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

attempt to index nil with 'leaderstats'?

Asked by
TriteA1 30
4 years ago

Beginner scripter trying to make a script that adds to a Variable stored inside of a leaderstats folder. The script works perfectly for about 10-20 touches then gives off this error: attempt to index nil with 'leaderstats'.

Any help appreciated, thanks.

debounce = false

script.Parent.Touched:Connect(function(hit) 
    if not debounce then debounce = true
           local player = game.Players:GetPlayerFromCharacter(hit.Parent);

           player.leaderstats.Height.Value = player.leaderstats.Height.Value + 10
           wait(1) 
           debounce = false 

    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Make sure whatever touches it is a player

debounce = false

script.Parent.Touched:Connect(function(hit) 
    if not debounce then debounce = true
           local player = game.Players:GetPlayerFromCharacter(hit.Parent);
       if player then
            player.leaderstats.Height.Value = player.leaderstats.Height.Value + 10
       end
           wait(1) 
           debounce = false 

    end
end)

0
Thanks, why did it work now? The part was never touching anything anyway? TriteA1 30 — 4y
Ad

Answer this question