Delay on stopping Tool Animation

Hello! I am still learning to script, so please forgive me if my script is messy.

local Cooldown = false
local IsEquipped = false
local IsJumping = false
local IdleAnim = script.Parent.Handle.Animation:FindFirstChild("Idle")
local WalkAnim = script.Parent.Handle.Animation:FindFirstChild("Walk")
local JumpAnim = script.Parent.Handle.Animation:FindFirstChild("Jump")
local Trigger1Anim = script.Parent.Handle.Animation:FindFirstChild("Trigger1")
local TriggerJAnim = script.Parent.Handle.Animation:FindFirstChild("TriggerJump")

script.Parent.Equipped:Connect(function()
	local Idle = script.Parent.Parent.Humanoid:LoadAnimation(IdleAnim)
	local Walk = script.Parent.Parent.Humanoid:LoadAnimation(WalkAnim)
	local Jump = script.Parent.Parent.Humanoid:LoadAnimation(JumpAnim)
	local Trigger1 = script.Parent.Parent.Humanoid:LoadAnimation(Trigger1Anim)
	local TriggerJ = script.Parent.Parent.Humanoid:LoadAnimation(TriggerJAnim)
	local GetHumanoid = script.Parent.Parent.Humanoid
	Walk.Priority = Enum.AnimationPriority.Action
	Idle.Priority = Enum.AnimationPriority.Movement
	IsEquipped = true
	if IsEquipped == true then
		Idle.Looped = true
		Idle:Play()
	else
		Idle.Looped = false
		Idle:Stop()
	end
	GetHumanoid.Jumping:Connect(function()
		Jump.Priority = Enum.AnimationPriority.Action
		if IsEquipped == true then
			Jump:Play()
			IsJumping = true
		else
			Jump:Stop()
		end
	end)
	GetHumanoid.StateChanged:Connect(function(Jumping,Landed)
		if Jumping == Enum.HumanoidStateType.Jumping then
			if IsJumping == false then
				IsJumping = true
			end
		elseif Landed == Enum.HumanoidStateType.Landed then
			if IsJumping == true then
				IsJumping = false
			end
		end
	end)
	GetHumanoid.Running:Connect(function(IsWalking)
		if IsJumping == false then
			if IsWalking > 1 and IsEquipped == true then
				Walk.Looped = true
				Walk:Play()
			else
				Walk:Stop()
			end
		end
	end)
	script.Parent.Unequipped:Connect(function()
		IsEquipped = false
		Trigger1:Stop()
		TriggerJ:Stop()
		Jump:Stop()
		Idle:Stop()
		Walk:Stop()
	end)
end)

script.Parent.Activated:Connect(function()
	local Idle = script.Parent.Parent.Humanoid:LoadAnimation(IdleAnim)
	local Trigger1 = script.Parent.Parent.Humanoid:LoadAnimation(Trigger1Anim)
	local TriggerJ = script.Parent.Parent.Humanoid:LoadAnimation(TriggerJAnim)
	local GetHumanoid = script.Parent.Parent.Humanoid
	Trigger1.Priority = Enum.AnimationPriority.Action2
	TriggerJ.Priority = Enum.AnimationPriority.Action2
	if IsJumping == false then
		if Cooldown == false then
			GetHumanoid.WalkSpeed = 0
			Cooldown = true
			Idle:Stop()
			Trigger1:Play()
			script.Parent.Handle.Sounds:FindFirstChild("Whoosh"):Play()
			task.wait(0.7)
			GetHumanoid.WalkSpeed = 16
			Idle:Play()
			Cooldown = false
		end
	else
		if Cooldown == false then
			GetHumanoid.WalkSpeed = 0
			Cooldown = true
			Idle:Stop()
			TriggerJ:Play()
			script.Parent.Handle.Sounds:FindFirstChild("Whoosh"):Play()
			task.wait(0.7)
			GetHumanoid.WalkSpeed = 16
			Idle:Play()
			Cooldown = false
		end
	end
end)

For some odd reason, the Idle Animation won’t stop after I unequip the Tool (If it’s activated)
This bug doesn’t appear when I don’t activate the Tool; Here’s a video on what I mean:

1 Like

In your activation function, you stop the idle animation and continue playing it without checking if the tool is still equipped or not. One solution you can take advantage of is simply changing animation priorities and never stopping the Idle to begin with, or using your stored isEquipped boolean.

3 Likes

Thank you! I removed the line that stops and plays the idle animation when I activate the tool, and it worked!

Some tips to optimize your code:

  • Use a variable for an object you’re going to use many times. In your script you’re constantly referencing script.Parent, script.Parent.Parent, script.Parent.Parent.Humanoid. It would be a lot more readable if you stored these as a variable.
  • Don’t copy paste code with very small changes.

Here you could easily put most of it outside the if statement, and only put the Trigger1/J in there.

1 Like