Why is animation still playing when tool is unequipped?

My character is still playing the idle animation even though the tool is unequipped. This bug only occurs when the player spams to equip and unequip the tool. I have no clue what I’m doing wrong.

LocalScript inside of Tool which handles the animations:

Tool.Equipped:Connect(function()
	ToolEquipAnimation:Play()
		
	ToolEquipAnimation.Stopped:Connect(function()
		ToolIdleAnimation:Play()
	end)
end)

Tool.Unequipped:Connect(function()
	for _, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		if AnimationTrack.Name == "R15ToolIdle" then
			AnimationTrack:Stop()
		end
	end
end)
1 Like

Hi again .-.

Tool.Equipped:Connect(function()
	ToolEquipAnimation:Play()
		
	ToolEquipAnimation.Stopped:Connect(function()
		ToolIdleAnimation:Play()
	end)
end)

Tool.Unequipped:Connect(function()
	ToolIdleAnimation:Stop() --it looks like you have a variable already so this is what you need to do
end)

It still plays when tool is unequipped. I used a for loop to check all the playing animation tracks so that I could stop all the animation tracks from playing when unequipped but for some reason it still plays.

1 Like

I think its because its a lopped animation, make it normal and use a loop to play it until the parents of the tool is not the player character

2 Likes

This will make you loop work perfectly fine

ToolIdleAnimation seems to be an upvalue of both callback functions. Simply call Stop() on it directly.

1 Like

Sorry for late reply.

Yes the idle is a looping animation. I have overwritten the animation so it is not looping. How should I write the code?

You have to set the looping off or else it will not work.

I have already said that. The animation keeps playing but it stops after a few seconds. It just looks buggy.

Sry for late replye i was asleep, all you need to do for the animation is

local tool = script.Parent --Change this
loval ID 
local animation = tool.Parent.Humanoid:LoadAnimation(ID)

game:GetService("RunService").Heartbeat:Connect(function() --might need to change heartbeat to renderstepped
	if tool.Parent:IsA("Model") and game.Players:FindFirstChild(tool.Parent.Name) then
		animation:Play()
	else 
		animation:Stop()
	end
end)
4 Likes

i mean, if you unequip too fast isnt that will play idle after you try to stop idle anim?

No? It checks every frame if the tool is equipped and if the player is alive, if you unequip too quickly, it’ll be corrected next frame, basically nothing noticable.

oh right you did stop animation every single frame…

Please be aware that this method is not particularly efficient and there exists a more efficient and effective alternative, such as listening to parent changes only. The solution I gave was the one that was initially thought of at that time.

1 Like