Animation not stopping after unequipping tool

I’m having a issue where my tool animation keeps playing after it being unequipped, even though I tried to make it so it stops upon being unequipped.

It does not print out any errors in the output and I tried to use multiple Devforum posts to make it work but none of them helped me to achieve the result I wanted (the animation to stop)

Here’s the script:

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent.Parent.Parent
local handle = Tool.Handle
task.wait(0.2)
local EatAnimation = script.Parent.Parent.Parent.Animation
local IdleAnimation = script.Parent.Parent.Parent.Idle

Tool.Equipped:Connect(function()
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	Tool.Enabled = false
	cooldown = true
	local Character = Tool.Parent
	local AnimationTrack = Character:WaitForChild("Humanoid"):LoadAnimation(EatAnimation)
	AnimationTrack:Play()
	wait(3)
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	local idleTrack = Character:WaitForChild("Humanoid"):LoadAnimation(IdleAnimation)
	idleTrack:Play()
	cooldown = false
	Tool.Enabled = true
end)

Tool.Unequipped:Connect(function()
	local Character = Tool.Parent
	local idleTrack = Character:WaitForChild("Humanoid"):LoadAnimation(IdleAnimation)
	idleTrack:Stop()
end)

When you unequip the tool, you created a new animation track and then stopped that one. You didn’t stop the animation that was already running. You could try using

Tool.Unequipped:Connect(function()
	local Character = Tool.Parent
	for i, v in pairs(Character:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
		v:Stop()
	end
end)

or you could do something like this:

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent.Parent.Parent
local handle = Tool.Handle
task.wait(0.2)
local EatAnimation = script.Parent.Parent.Parent.Animation
local IdleAnimation = script.Parent.Parent.Parent.Idle
local idleTrack -- create a blank variable

Tool.Equipped:Connect(function()
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	Tool.Enabled = false
	cooldown = true
	local Character = Tool.Parent
	local AnimationTrack = Character:WaitForChild("Humanoid"):LoadAnimation(EatAnimation)
	AnimationTrack:Play()
	wait(3)
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	idleTrack = Character:WaitForChild("Humanoid"):LoadAnimation(IdleAnimation)
	idleTrack:Play() -- change the variables value
	cooldown = false
	Tool.Enabled = true
end)

Tool.Unequipped:Connect(function()
	local Character = Tool.Parent -- Edit: I noticed this line might refer to the Players Backpack because it's unequipped. You might want to fix that.
	if idleTrack then -- checks if the animation exists
		idleTrack:Stop()
		idleTrack = nil -- sets the variable back to nil
	end
end)

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