How do you get the length of an animation? [ H E L P ]

I am trying to use adjustspeed property of animationtrack to make it play a certain speed and part of that speed value uses animationtrack.Length and when i try to access animationtrack.Length it only returns a value of 0 which is my problem here. It says on developer hub

“A read only property that returns the length (in seconds) of an AnimationTrack . This will return 0 until the animation has fully loaded and thus may not be immediately available.”

Ok then how do you “Fully load” this animation? cause i created an animationtrack with it’s animation ID is that not considered loading it? i even waited in game for a while and it still didnt work. So whats the big idea?

yay everyone else’s posts gets 0 views but 999 helpful comments while i get 0 views and 0 comments

1 Like

Try to preload the animation using ContentProvider:

local ContentProvider = game:GetService("ContentProvider")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Animation = script:WaitForChild("Animation")

local Anim = Humanoid.Animator:LoadAnimation(Animation)

ContentProvider:PreloadAsync({Animation})

print(Anim.Length)
3 Likes

It still says “0” after doing what you said

local ContentProvider = game:GetService("ContentProvider")

local function loadAnimation(animID,char)
	local anim = Instance.new("Animation",char)
	anim.AnimationId = "rbxassetid://" .. tostring(animID)
	local loadedAnim = animator:LoadAnimation(anim)
	
	ContentProvider:PreloadAsync({anim})
	return loadedAnim
end

local vault = loadAnimation(id,char)

print(vault.Length)
1 Like

This is an annoying issue, here’s a method by @sircfenner that I use at times like this:

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
2 Likes

i tried it and it returns “0”, btw im in a clientside script if that has anything to do with it

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


local function loadAnimation(animID,char,name)
	local anim = Instance.new("Animation",char)
	anim.Name = tostring(name)
	anim.AnimationId = "rbxassetid://" .. tostring(animID)
	local loadedAnim = animator:LoadAnimation(anim)
	return loadedAnim
end

local vault = loadAnimation(ID,char,"Vault")

local x = GetAnimationLength(char.Vault)
print(x) -- Prints 0
1 Like

is it not working because I am using Humanoid.Animator?

1 Like

Try this twice on ClientSide: (command bar)

local Animation = workspace.Animation

local character = game:GetService("Players").LocalPlayer.Character
local Humanoid = character.Humanoid

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

this should work.

8 Likes