Animation not Stopping on Unequip

Hello! I was trying to make an animated sword tool, everything works according to plan, but a weird phenomenon happens, I put a video below showing it:

I have done all the measures taken in other topics, however, it has still not worked.
My code stops ALL animations from occurring when the tool is unequipped, but it still lingers.

It only happens when the tool is unequipped when the equipTrack animation has not finished. It works fine if the idleTrack animation has started.

The code mentioned above is here, running in a LocalScript.

local player = game.Players.LocalPlayer
local animator = player.Character:WaitForChild("Humanoid"):FindFirstChildOfClass("Animator")
local equipTrack = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Equip)
local idleTrack = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Idle)
local swing1Track = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Swing1)
local swing2Track = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Swing2)

local cooldown = false

script.Parent.Equipped:Connect(function()
	equipTrack:Play() --Play equipping animation
	script.Parent.ToolEquipped:FireServer() 
	equipTrack.Stopped:Connect(function() --Wait until animation finishes
		idleTrack:Play() --Play idle track
	end)
end)

script.Parent.Unequipped:Connect(function()
	for i,v in ipairs(animator:GetPlayingAnimationTracks()) do
		v:Stop()
	end
end)

script.Parent.Activated:Connect(function()
	local randState = math.random(1,2)
	if randState == 1 and cooldown == false then --Play swing animations at random
		swing1Track:Play()
		script.Parent.ToolActivated:FireServer() --Fire activation event
		cooldown = true --Set cooldown to true
		swing1Track.Stopped:Wait()
		cooldown = false --Reset cooldown
		script.Parent.ToolActivationComplete:FireServer()
 	elseif randState == 2 and cooldown == false then
		swing2Track:Play()
		cooldown = true --Same as above
		script.Parent.ToolActivated:FireServer()
		swing2Track.Stopped:Wait()
		cooldown = false
		script.Parent.ToolActivationComplete:FireServer() --Fire activation event
	end
end)

did you try adding any print statements yet?

To see if there is a spot in the code not running

1 Like

I have, it seems the for loops is always running when the tool is equipped.

Just wanted to say that doing this could cause a potential memory leak, as the connection isn’t being disconnected after it has been triggered. You should replace this connection with:

	equipTrack.Stopped:Wait()
	idleTrack:Play() --Play idle track

You can simply add a check before playing the idleTrack on your script.Parent.Equipped function to check whether the tool is in the character:

	if not script.Parent.Parent.Parent:FindFirstChildOfClass("Humanoid") then return end

Wow! I did not know that using the :Connect method could cause memory leaks, thanks for notifying me!

Your solution works perfectly! Thank you for reply very quickly!

If it does not burden you, could you explain why this happens?

When you ran this for loop, it stops the equipTrack from playing, which triggers the function to play the idleTrack. Since the list of playing animations the for loop used (animator:GetPlayingAnimationTracks()) was cached, it didn’t automatically update itself to include the idleTrack that just played, hence the bug.

Thanks for the explanation! I will try and use non-deprecated functions in the future. :slight_smile:

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