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

attempt to index nil with 'leaderstats'?

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

✓ Accepted answer

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)

1 comment

0

Thanks, why did it work now? The part was never touching anything anyway?

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