Animation isn't waiting long enough exactly?

So I have 2 animations. An action swinging animation, and an idle animation holding the sword above my head.

I can have the idle animation stop, and then play after the swinging animation is finished. But without a “task.wait” it doesn’t exactly let the swing animation do it’s “whole” animation of swinging down.

tool.Activated:Connect(function()
	if debounce == false then
		debounce = true
		
		if idle then
			idle:Stop()
		end

		tool.Parent:FindFirstChild("Humanoid").Animator:LoadAnimation(animationSwing):Play()
	
	task.wait(0.3)
		 
		if idle then
			idle:Play()
		end
		
		debounce = false
	end
end)

This lets the swinging animation go all the way down, but only because I added the “task.wait(0.3)” This could look weird if the timing isn’t exactly perfect. Is there any other way make sure the swing has actually does it’s entire animation properly before the Idle animation plays again? (Without using a task.wait)

  1. Learn about animation priorities. Set the idle animation to the “Idle” Priority and the swinging animation to the “Movement” / “Action” priority. Keep the idle animation looping and the swing animation non looping.
idle:Play() -- play idle forever since it loops

swing = tool.Parent:FindFirstChild("Humanoid").Animator:LoadAnimation(animationSwing)
function attack()
	swing:Play() -- swing has higher priority so it will replace the idle animation while active
	-- no need to clean up swing, swing will terminate on its own since it does not loop
	-- once swing completes it smoothly goes back to idle
end

  1. If you can’t use animation priorities because of a really rare/specialized case, you’d use the animation ended event to trigger the idle to play again in a local/client script.
function attack()
	idle:Stop()
	swing:Play()
	swing.Ended:Wait() -- don't continue code until swing ends
	idle:Play()
end

I already set Priorities on both of the animations with idle looping, but it just looks like this:

https://gyazo.com/12cb6fcdf3bcf7b910bfd51afea72124

When I set the time.wait: (What it’s suppose to look like)

https://gyazo.com/9e41a6bb1cacba2ce37a0d0c75ead649

And when I do stop the idle animation, and do play the swinging animation, and then do play it again right after. The swinging animation still doesn’t seem to go all the way down.

To be honest my type of game I’m making has people be different sizes and stuff. So I might just keep the task.wait and make it longer someone is “taller/wider”

Yes, you need to set the swing animation to the “Action” or “Movement” priority instead of “Idle”.

Since you have 2 idle animations playing it’ll play the average of them both which is bad.

1 Like

You can do this by utilizing the AnimationTrack.Ended event. This triggers when an animation finishes playing, allowing you to resume the idle animation.

tool.Activated:Connect(function()
    if not debounce then
        debounce = true

        if idle then
            idle:Stop()
        end

        local humanoid = tool.Parent:FindFirstChild("Humanoid")
        local animator = humanoid and humanoid:FindFirstChild("Animator")
        if animator then
            local swingAnimation = animator:LoadAnimation(animationSwing)
            swingAnimation:Play()

            swingAnimation.Ended:Connect(function()
                if idle then
                    idle:Play()
                end
                debounce = false
            end)
        end
    end
end)
2 Likes

Thank you, this work’s perfectly!

1 Like

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