Animation still looping after tool unequip

While attacking, if the player unequips the tool, the player will stop attacking but the looping animation will play right after. Tried putting a wait() but if the player is fast enough, it will still play the animation.

Before:


After:

Here is the LocalScript.

local parent = script.Parent
local Handle = parent:WaitForChild("Handle")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

-- Ensure that the character's humanoid contains an "Animator" object
local Humanoid = character:WaitForChild("Humanoid")
EquipAnim = parent.Animations.Equip
IdleAnim = parent.Animations.Idle
Attack = parent.Animations.Attack
local Equip = Humanoid:WaitForChild("Animator"):LoadAnimation(EquipAnim)
local Idle = Humanoid:WaitForChild("Animator"):LoadAnimation(IdleAnim)
local Attack = Humanoid:WaitForChild("Animator"):LoadAnimation(Attack)
CanDmg = false
Can = true
cd = 0.44



parent.Unequipped:Connect(function()
	Unequip()
end)
function Unequip()
	if Attack.IsPlaying  then
		Attack:Stop()
	end
	if Idle.IsPlaying then
		Idle:Stop()
	end
end

parent.Equipped:Connect(function()
	Handle.Equip:Play()
	Equip:Play()
	wait()
	Idle:Play()
end)

parent.Activated:Connect(function()
		if Can == true then
			Can = false
			Humanoid.JumpPower = 0
			if Equip.IsPlaying then
			Equip:Stop()
			end
			if Idle.IsPlaying then
			Idle:Stop()
			end
			Attack:Play()
			wait(.36)
			Humanoid.JumpPower = 60.999
			Idle:Play()
			wait(cd)
			Can = true
		end
end)
1 Like
			if Idle.IsPlaying then
			Idle:Stop()
			end
			Attack:Play()
			wait(.36)
			Humanoid.JumpPower = 60.999
			Idle:Play()
			wait(cd)
			Can = true

The problem is here. The wait(.36) is the culprit of your problem. It will wait .36 seconds after you click activated and play an animation. If the player has the tool equipped and activates the tool, then unequips the tool before the wait(.36) is over, the idle animation will play despite the tool not being equipped.

To fix, check if the tool is equipped before playing the animation.

			if Idle.IsPlaying then
			Idle:Stop()
			end
if isToolEquipped == true then
			Attack:Play()
end
			wait(.36)
			Humanoid.JumpPower = 60.999
if isToolEquipped == true then
			Idle:Play()
end
			wait(cd)
			Can = true

Excuse the horrible formatting… but you see what I am getting at

1 Like

In your Unequip() function, you are not checking to stop the Equip animation.

This likely isn’t what is causing the issue, but is a possible edge case.

I believe @triixys has solved your animation issue.

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