How to know if animation has finished completely?

Hey.

I was basically want to find out when an animation has finished playing (played all the way through).

Using the AnimationTrack.Stopped event fires whenever the animation has stopped, for whatever reason (or am I wrong?)

Like so:

It’s unclear how this really works, for me atleast. Thanks for any answers, and happy new year!

3 Likes

The AnimationTrack.Stopped event triggers whenever your animation stops (or hit the last keyframe).
As said by the screen, it is useful when you are trying to chain a series of animations or when you are trying to add visuals.

What you could do is:

local animation = script:WaitForChild("Animation")
local playerCharacter = game.Players.LocalPlayer.Character
local humanoid = playerCharacter:WaitForChild("Humanoid")

local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()

animationTrack.Stopped:Wait()
print("The animation stopped.")
8 Likes

Does it fire when the animation get’s canceled too?

Thanks for answering!

I believe it fires when the animation is cancelled, yes.

You can still try by adding animationTrack:Stop() and see if it fires the .Stopped function.
(Make sure to mark my message as the Solution if it helped you, to close the topic.)

1 Like

Ok, thanks. I’m going to test it out.

Because I only want it to fire when it has played ALL THE WAY through.

Thanks so far.

If you want to check when your animation has finished playing entirely, definitively use animationTrack.Stopped:Wait().

The problem with that, though, is that the whole script will yield (indefinitely) if the animation gets canceled (as in, NOT completed).

To my knowledge, a player can’t force-stop an animation unless they sit down.

Yeah, that is correct.
However, in my game, I let the player cancel the action (if they want to), and I would want to know whether or not they actually did the whole action/animation or canceled it before it started. (Preferably without using the :Wait().)

Again, thanks for the help. I appreciate it.

If .Stopped doesnt work, you could try this.

repeat task.wait() until not animationTrack.IsPlaying

1 Like

you could use another variable when stopping the animation to determine that; for example

local forcedStop
animationTrack.Stopped:Connect(function()
      if forcedStop then
            --the animation didnt finish playing
      else
            -- the animation finished playing
      end
end)

forcedStop = true
animationTrack:Stop()

4 Likes