Help with tool Equip and Idle animations not breaking from spam

Hey there!

I have now spent 3, 1/2 hours trying to create my script from scratch. My main problem is that when I spam the tool, the second animation that plays will get stuck and play forever, even after the tool has been unequipped. Another issue is if I were to play both animations at the same time, the second one would overlap the first one for a split second even if the Action level is level 4.

If anyone can PLEASE help me create or give me help on how I can fix it so the tool animation doesn’t get stuck when spammed equip/unequipped fast, that’d be so helpful.

This is what I currently have:

local tool = script.Parent
local anim = Instance.new("Animation")
local anim2 = Instance.new("Animation")
anim.AnimationId = 'https://www.roblox.com/Assest?ID=16959719719'
anim2.AnimationId = 'https://www.roblox.com/Assest?ID=16942938568' --Id here
local track
local track2
 
tool.Equipped:Connect(function() 
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim) 
	track.Priority = Enum.AnimationPriority.Action2
	track:Play(0.6)
	
	tool.Handle.Transparency = 1
	wait(0.1)
	tool.Handle.Transparency = 0
	
	track.Stopped:Wait()
	tool.Main.Enabled = true
	
	track2 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(anim2) 
	track2.Priority = Enum.AnimationPriority.Action
	track2:Play(0.2)
	
end)
 

tool.Unequipped:Connect(function() 
	if track then
		track:Stop()
		track:Destroy()
		track = nil
	end
	if track2 then
		track2:Stop()
		track2:Destroy()
		track2 = nil
	end
end)
  • The issue is with the track2, for some reason if I were to spam the tool that animation would break and continue to stay after I unequip the tool. I’ve tried to do IsPlaying but it doesn’t seem to fix it.

Much appreciated!
xXRedFlamyXx

Put a debounce in there to control spamming the keys.

2 Likes

I don’t know if that’d work or not; nor where I’d put it.

Do I make the debounce active after track:Play(0.6)
Or where do I put this Debounce?

The issue with your second animation playing indefinitely is caused by that “wait” in the equip event. What happens is that two events happen at the same time if you spam the tool. Eventually the unequip event is fired before an equip event is finished, therefore keeping the animation on forever.

The simplest thing that comes to my mind rn (without having to change the code) is to remove the looping animation setting from roblox animation editor (i guess that’s the case, if it’s not lmk)

It’s looped because it’s the idle holding animation after the equip.

Is there any other way to fix this BY changing my code? I want this to be fixed.

Add some sort of animation handler: keep track of the track (sorry for the wordplay) loaded into the humanoid so that you have full control over it. Something like this at the start of your code:

local anim = Instance.new("Animation")
local char = script.Parent.Parent // From what I can see from your code

// First Animation
anim.AnimationId = 'https://www.roblox.com/Assest?ID=16959719719'
local track1 = char.Humanoid:LoadAnimation(anim)
// Second one
anim.AnimationId = 'https://www.roblox.com/Assest?ID=16942938568'
local track2 = char.Humanoid:LoadAnimation(anim)

Then you use those two variable in the rest of the code without checking for their validity or loading (so just :play() and :stop())

then you can add a check when playing the second animation to see if the tool is equipped, and if it’s not don’t play it

If I play both of them at the same time, when I equip the tool there is a split second that the Idle animation plays before the equip, no matter the action level, making the animations look weird. That’s why there is a wait between them both.

didn’t say to play them at the same time, just not to load them during the event, your code would look something like this

local tool = script.Parent
local anim = Instance.new("Animation")
local char = script.Parent.Parent // From what I can see from your code

// First Animation
anim.AnimationId = 'https://www.roblox.com/Assest?ID=16959719719'
local track1 = char.Humanoid:LoadAnimation(anim)
// Second one
anim.AnimationId = 'https://www.roblox.com/Assest?ID=16942938568'
local track2 = char.Humanoid:LoadAnimation(anim)
track2.Priority = Enum.AnimationPriority.Action

tool.Equipped:Connect(function() 
	track1:Play(0.6)
	tool.Handle.Transparency = 1
	wait(0.1)
	tool.Handle.Transparency = 0
	
	track1.Stopped:Wait()
	tool.Main.Enabled = true
-- Put a condition checking if the tool is equipped here, if it is then play it
	track2:Play(0.2)
end)
 

tool.Unequipped:Connect(function() 
    track1:Stop()
    track2:Stop()
end)

Is it just if tool.Equipped then or how do I create the if equipped?

remember to check the forum, especially if it’s something that sounds popular (like ‘how do i check if a tool is equipped’)

The animations do not play.


add a if script.Parent.Parent:FindFirstChild("Humanoid") then at the top of the code (the script is present both in the player backpack and in the character model, if i do remember correctly, this allows you to filter out the first possibility)