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

Attempt nil to index with leaderstats error?

Why wont this script work? Most of it does but it does not change the height leaderstat to + 1 like it should.

local debounce = false
script.Parent.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
	if hum and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) and not debounce then
		debounce = true
		hum.BodyHeightScale.Value = hum.BodyHeightScale.Value + 0.1

		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		task.wait(5)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		debounce = false
		
		game.Players.LocalPlayer.leaderstats.Height.Value = game.Players.LocalPlayer.leaderstats.Height.Value + 1
		
	end
end)

Thanks!

1 answer

✓ Accepted answer

You can't use LocalPlayer in a ServerScript. Also, try to avoid nesting, whenever you can.

This should work:

local debounce = false
script.Parent.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not hum and plr then return end
	if debounce == true then return end
	debounce = true
	hum.BodyHeightScale.Value += .1
	plr.leaderstats.Height.Value += 1
	script.Parent.Transparency = 1
	script.Parent.CanCollide = false
	task.wait(5)
	script.Parent.Transparency = 0
	script.Parent.CanCollide = true
	debounce = false
end)

5 comments

0

thsi sorta works but when i pickj up the orb more than 2 times, nothing happens and it says an error with the bodyheight scale

0

thsi sorta works but when i pickj up the orb more than 2 times, nothing happens and it says an error with the bodyheight scale

0

can u post the exact error

0

its working for me, your problem is that ur part is not anchored

0

I agree with you on the never-nesting ideology, but mushing every line together harms readability far more than the existing layers of indentation

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