Statically retrieve animation lengths

As a Roblox developer, it is currently impossible to know the absolute length of an animation without first loading it into a humanoid.

Further frustrations arise when you realize that humanoid:LoadAnimation() doesn’t yield and the returned AnimationTrack has length set to 0 - meaning it is possible to read an incorrect animation length if you aren’t careful. We also have no way of knowing whether an animation track has actually loaded, meaning we have to rely on a hacky method (polling for length > 0 updates) for this to work.

This isn’t very clear in the documentation, and can lead to a lot of frustrating moments while loading animations. (ex: this thread How can I know when an AnimationTrack is loaded?, where a user is confused about animationtracks and their loading behavior).

There should be some way to retrieve animation lengths without having to load the entire animation first (or atleast some better behavior with respect to yielding).

7 Likes

In the meantime, here’s a (less hacky) method you can use with better yielding behavior than what you described:

local Provider = game:GetService("KeyframeSequenceProvider")
local Cache = {}

local function GetAnimationLength(Animation)
	local AssetId = Animation.AnimationId
	if Cache[AssetId] then
		return Cache[AssetId]
	end

	local Sequence = Provider:GetKeyframeSequenceAsync(AssetId)
	local Keyframes = Sequence:GetKeyframes()
	
	local Length = 0
	for i = 1, #Keyframes do
		local Time = Keyframes[i].Time
		if Time > Length then
			Length = Time
		end
	end
	
	Sequence:Destroy()
	Cache[AssetId] = Length
	
	return Length
end
8 Likes

For anyone using this:
Be warned that the line

Sequence:Destroy()

can cause Roblox to destroy its local cached keyframes from the animation, making future references to AnimationTrack.Length equal to 0 (along with some other potential problems)

If you want to ever use AnimationTrack.Length, you should comment out

Sequence:Destroy()

I went through many headaches before figuring this out :frowning:

It’d really be nice if Roblox added official support for this functionality, so we wouldn’t have to use such hacks with unintended side effects.

9 Likes

Being able to store the length of an animation without having to load it into a humanoid would be incredibly useful.

My current issue is that both the client and server need to know the length of melee animations. This is no issue on the client as the animations need to be loaded there anyway.

On the server though, this is a different matter. I don’t need to load the animations on the server as I only play them on the client but I’ll have to because there’s no other way to access the length of the animations. Of course I could use a RemoteFunction to get the lengths from the client, but an exploiter cann delete the LocalScript and implement his own and return wrong values with benefits him and negatively impacts others.

7 Likes

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