Very simple tool script is not working for me. Backpack.Run.Script:10: attempt to index nil with 'WalkSpeed'

I have made a tool that, when equipped, makes you walk faster, and then makes you walk slower after unequipping. The problem is that I get errors when I unequip the tool.

Backpack.Run.Script:10: attempt to index nil with 'WalkSpeed'

It seemed obvious at first; the tool was sent to the backpack after unequipping and therefore couldn’t check the humanoid from there, however, I had configured the script to save the humanoid in a variable but it still doesn’t work.

Here is the script:

local tool = script.Parent
local character
local humanoid
local debounce = true

function equipped()
	if tool.Parent then
		character = tool.Parent
		humanoid = character:FindFirstChildOfClass("Humanoid")
		humanoid.WalkSpeed = 32
	end
end

function unequipped()
	humanoid.WalkSpeed = 16
end

tool.Equipped:Connect(equipped)
tool.Unequipped:Connect(equipped)

I feel bad for bringing this up here since it sounds like a really simple issue, but I really don’t know how to fix this. I welcome any help on fixing this.

1 Like

On the very last line, you accidentally connect the wrong function to the Unequipped event.

2 Likes

Agh! You’re completely right. I have corrected that mistake, and now the script works perfectly as intended. Such a silly err. Thank you for pointing this out.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.