Hi Developers,
We’re excited to release AnimationTrack.Ended
- a new event on the AnimationTrack instance.
Currently, AnimationTrack.Stopped event work as intended. “Stopped” fires the moment that AnimationTrack decides to “wind down”, e.g. after calling AnimationTrack:Stop. Depending on the length of the fadeTime
argument, the animation will continue to move its subject until the subject returns to a neutral pose.
Without an API change, there is no way to know for sure that an AnimationTrack is finished affecting the world. We want creators to be able to detect that as well when an AnimationTrack is finished moving its subject.
That’s where AnimationTrack.Ended
comes in.
Solution
When AnimationTrack.Ended
fires, the AnimationTrack is “completely done” and will no longer be moving anything at all.
This is useful for cases where you want to perform an action (e.g. destroying the animation track) only when it is finished affecting the world.
Sample code:
– I want the animation to fade out over 5 seconds. Then, and only then, I want to clean it up.
animationTrack.Stopped:Connect(function()
– The animation track is still moving the subject!
– Don’t clean things up here.
print("Stopped Event fired")
end)
animationTrack.Ended:Connect(function()
– The animation track is completely finished moving things.
– Now we can safely clean things up.
print("Ended Event fired")
animationTrack:Destroy()
animationTrack = nil
end
print("Calling animationTrack:Stop")
animationTrack:Stop(5)
The debug would be:
Calling animationTrack:Stop
Stopped Event fired
Ended Event fired
(Note: There’s a five second delay between printing second and third lines).
Alternate Solutions without using AnimationTrack.Ended
You could use existing API to achieve something like this:
animationTrack.Stopped:Connect(function()
task.wait(5)
animationTrack:Destroy()
animationTrack = nil
end)
animationTrack:Stop(5)
But:
-
Using
task.wait
is generally a code smell, to be avoided. You don’t need it with Ended event. -
Depending on the complexity of your code, at the point you handle the “Stopped” event you might not know the
fadeOut
param that was passed into Stop. -
There are no guarantees that the
fadeOut
will take exactly time passed in. You might wind up a few frames ahead of or behind the actual time when the AnimationTrack stopped affecting the world. The “Ended” event is guaranteed to be precise.
Please share your feedback down below and let us know if you have any questions
Thank you.