Attempt to index nil with humanoid

I Was trying to make an animation script of the character holding the tool that when the tool is equipped the humanoid will play an animation.
but when I unequip the tool the animation wont stop playing.
I’ve tried changing some lines and some stuff in the studio and I Have noticed that when it unequips it goes to the character’s backpack, how can I get the humanoid from there?
script:

local humanoid = nil
local idle = nil
script.Parent.Equipped:Connect(function()
	local humanoid = script.Parent.Parent.Humanoid
	local idle = humanoid:LoadAnimation(script.Parent.Idle)
	idle:Play()
	wait(1)
	end)
script.Parent.Unequipped:Connect(function()
	local character = game.Workspace:FindFirstChildOfClass("Character")
	local h = character.Humanoid
	idle:Stop()
	wait(1)
end)

“Character” is not a proper class name

This wouldn’t work since there’s no class called Character. Also when it’s uneqipped, can’t you just do

local character = script.Parent.Parent.Parent.Character

Since it’ll be like this

script → tool → backpack → player

Or alternatively, just set it when it’s equipped and don’t tryto get it when it’s unequpped. You alreayd have the humanoid from Equipped, just remove the local from it and you should be set

The local from this line

local humanoid = script.Parent.Parent.Humanoid

Basically I’m saying the code should be like this

local humanoid = nil
local idle = nil

script.Parent.Equipped:Connect(function()
	humanoid = script.Parent.Parent.Humanoid
	idle = humanoid:LoadAnimation(script.Parent.Idle)
	idle:Play()
	wait(1)
end)

script.Parent.Unequipped:Connect(function()
	idle:Stop()
	wait(1)
end)

You already made variables for humanoid and idle, no need to try to get the character in Unequipped to get the humanoid when it’s already given via the Equipped

1 Like