How can I fix tool animations after unequip?

So basically when I equip the gun tool and I sprint and jump and unequip then it keeps doing the walking and idle animation.

here is my code:

script.Parent.Equipped:Connect(function()
	local character = script.Parent.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if not humanoid then return end

	-- Load animations
	local idle = humanoid:LoadAnimation(script.Idle)
	local walk = humanoid:LoadAnimation(script.Walk)
	local sprint = humanoid:LoadAnimation(script.Sprint) -- Sprint animation
	local jump = humanoid:LoadAnimation(script.Jump)    -- Jump animation
	idle:Play()

	-- State variables
	local equipped = true
	local walking = false
	local sprinting = false
	local jumping = false

	-- Monitor jumping
	humanoid.StateChanged:Connect(function(_, newState)
		if newState == Enum.HumanoidStateType.Jumping then
			if not jumping then
				jumping = true
				jump:Play()
				idle:Stop()
				walk:Stop()
				sprint:Stop()
			end
		elseif newState == Enum.HumanoidStateType.Landed then
			if jumping then
				jumping = false
				jump:Stop()
				if humanoid.MoveDirection.Magnitude > 0 then
					if humanoid.WalkSpeed >= 14 then
						sprint:Play()
						sprinting = true
					else
						walk:Play()
						walking = true
					end
				else
					idle:Play()
				end
			end
		end
	end)
	
	local function disableAnimations()
		idle:Stop()
		walk:Stop()
		sprint:Stop()
		jump:Stop()
		print("Disabled All Animations")
	end

	-- Coroutine to handle walking and sprinting
	coroutine.wrap(function()
		while equipped do
			wait(0.1)
			if not jumping then
				if humanoid.MoveDirection.Magnitude > 0 then
					if humanoid.WalkSpeed == 22 then
						if not sprinting then
							sprinting = true
							walking = false
							idle:Stop()
							walk:Stop()
							sprint:Play()
						end
					else
						if not walking then
							walking = true
							sprinting = false
							idle:Stop()
							sprint:Stop()
							walk:Play()
						end
					end
				else
					if walking or sprinting then
						walking = false
						sprinting = false
						walk:Stop()
						sprint:Stop()
						idle:Play()
					end
				end
			end
		end
	end)()

	-- Unequipped event
	script.Parent.Unequipped:Once(function()
		disableAnimations()
		sprinting = false
		walking = false
		jumping = false
	end)
end)```


Any way I could fix this?
1 Like

Im not sure, but I think I found the error:

In the coroutine, theres a while loop thats checking if the tool is equipped. You are never setting the equipped bool off, so the while function is always running. The idle animation is still playing, since this part:

if walking or sprinting then
   walking = false
   sprinting = false
   walk:Stop()
   sprint:Stop()
   idle:Play()
end

fires once after unequipping. I think you forgot to set equipped to false after unequipping.

1 Like

i tried what you said but now instead it is sprinting instead of walking and it is not idling

1 Like

Thats wierd. Did you type equipped = false in script.Parent.Unequipped:Once()? or did you put it somewhere else. Check again, the behavior shouldnt have changed.

1 Like

that is where I put it inside of the unequipped function

1 Like