Unequip animation not being played when unequipping tool

  1. What do you want to achieve?
    I want to stop all animations when a tool is unequipped. (So idle, walking, jumping animations etc can run)
  2. What is the issue?
    When I unequip the tool, the holding animation still plays.
  3. What solutions have you tried so far?
    I have tried calling the Stop method on the animation track for the holding animation in the onUnequipped function, but it doesn’t seem to be working.

Here is my current code:

local Tool = script.Parent
local enabled = true
local pickUpAnim = Tool.Animations:WaitForChild("Tool PickUp")
local holdingAnim = Tool.Animations:WaitForChild("holding")
local unequipAnim = Tool.Animations:WaitForChild("Tool Unequip")
local animTrack1 = nil
local animTrack2 = nil

function onActivated()
	local player = game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
	local thirst = player:WaitForChild("playerstats"):WaitForChild("moods"):WaitForChild("Thirst")
	local thirstIncrease = 17
	local timesDrinked = Tool:WaitForChild("TimesDrinked")
	if not enabled or timesDrinked.Value >= 3 then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(0,-.759,-.651)
	Tool.GripPos = Vector3.new(1.5,-.5,.3)
	Tool.GripRight = Vector3.new(1,0,0)
	Tool.GripUp = Vector3.new(0,.651,-.759)

	Tool.Handle.DrinkSound:Play()

	if timesDrinked.Value >= 3 then
		Tool:Destroy()
	else
		task.wait(3)

		local h = Tool.Parent:FindFirstChild("Humanoid")
		if h ~= nil then
			if h.MaxHealth > h.Health + 5 then
				h.Health = h.Health + 5
			else
				h.Health = h.MaxHealth
			end
		end

		Tool.GripForward = Vector3.new(-.976,0,-0.217)
		Tool.GripPos = Vector3.new(0.03,0,0)
		Tool.GripRight = Vector3.new(.217,0,-.976)
		Tool.GripUp = Vector3.new(0,1,0)
	end

	task.wait(1.5)

	-- Increase the player's thirst by the specified amount if it is not already at the maximum of 100
	if thirst.Value < 100 then
		thirst.Value = math.min(thirst.Value + thirstIncrease, 100)
	end

	-- Increase timesDrinked by 1
	timesDrinked.Value = timesDrinked.Value + 1

	enabled = true
end

function onEquipped()
	Tool.Handle.OpenSound:Play()
	local pickUpAnim = Tool.Animations:WaitForChild("Tool PickUp")
	local holdingAnim = Tool.Animations:WaitForChild("holding")
	local humanoid = Tool.Parent:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")

	-- Only load the animation tracks if they don't already exist
	if not animTrack1 then
		animTrack1 = animator:LoadAnimation(pickUpAnim)
	end
	if not animTrack2 then
		animTrack2 = animator:LoadAnimation(holdingAnim)
	end

	-- Play the pickUp animation once
	animTrack1:Play()
	animTrack1.Stopped:Wait()

	-- Play the holding animation
	animTrack2:Play()
end

function onUnequipped()
	local unequipAnim = Tool.Animations:WaitForChild("Tool Unequip")
	local humanoid = Tool.Parent:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	local animTrack = animator:LoadAnimation(unequipAnim)

	-- Stop the pickUpAnim and holdingAnim animations
	if animTrack1 then
		animTrack1:Stop()
	end
	if animTrack2 then
		animTrack2:Stop()
	end

	animTrack:Play()
	task.wait(0.5)
	animTrack.Stopped:Wait()
end

Tool.Activated:Connect(onActivated)
Tool.Equipped:Connect(onEquipped)
Tool.Unequipped:Connect(onUnequipped)

You defined the animTrack variable, but you never played it on the onUnequipped function.

Also, why defining it to nil since you already destroyed it?

I did try playing the animTrack animation in the onUnequipped function, but it wasn’t working aswell. I apologize for any confusion - I accidentally included the wrong script in my original post. The script I provided was one that I was doing some tests with. I set animTrack1 and animTrack2 to nil after destroying them to see if by doing that atleast something would happen, but the holding animation kept playing. (I already edited the post including the correct script).

If the animTrack1 is the “pickUpAnim” and is played when the player equips the tool, and its not looped, I guess you dont need to stop it in the unequip function.

Here you are yielding the current thread until 0.5 seconds, and after you waits until the track has stopped. If you just want to wait the animation to stop before playing it again, you dont need the task.wait ig.

Also, you can try debugging the Unequip function using print() or warn().

I managed to fix it. I didn’t found the problem in my script, but after searching a lot, I found this post, which managed to help me.

Going to leave it here just in case someone struggles with the same issue as me.

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