AnimationTrack.Length not accurate

So I’m trying to make an NPC that does random dance moves (animations) and then once that animation is finished (wait(track.Length)), the script is supposed to play a new animation.
My problem is that the script does not wait the actual length of the animation track and the animations overlap and give a glitchy dance.
I tried setting track.Looped = false and that still didn’t do anything.
Any suggestions on what I can do to fix the issue?
Thank you for your feedback.

Edit: I tried a print(track.Length) and the output was 0 for most of the animations.

1 Like

Try this: (task.wait(track.Length + 0.2))

I tried that but it still overlapped the animations

save the animation after you play it and then just do

animation.Ended:Connect(function()

end)

or
animation.Ended:Wait()

or
animation.Ended:Once(function()

end)

so something like this:

local animation = "loaded animation here"

animation.Ended:Connect(function()
-- fire a function or smth 
end)
5 Likes

The animations usually takes time to load before returning the real length

local Anim = Humanoid:LoadAnimation(Animation)
repeat task.wait() until Anim.length ~= 0

--print(track.Length)
--script here
1 Like

The problem is that the lengths sometimes return a number slightly bigger than 0, such as 0.5237 etc.

Try to tamper with the repeat loop like this if it doesn’t work exactly the way you expected:

repeat task.wait() until Anim.length > 1

If you’re still encountering it returning a fake length, why not use ContentProvider:PreloadAsync()?

1 Like

I had a similar problem, and had actually used the ContentProvider to Preload all my anims but i had encountered a weird thing - the anims played normally but when i tried to use Anim.Length the first time the animation played the output was 0 and the next time it played it was normal. The line of code above has solved it

this here, though id replace the 1 with a 0. the idea is to wait for the animation to finish loading before taking its length and running the wait statement.
alternatively, you can do AnimationTrack.Stopped:Wait() where animationtrack is your loaded animation