Trouble with getting the length of an animation

So I have created a script that should get the length of an animation and yes I know that the length of the animation isnt loaded instantly so I made something that will make it wait to it does.

Anyhow I do this and this should trigger the length is greater than 0, and when that happens it prints 0.
Why?

local loaded = animator:LoadAnimation(anim)
		
		local z = 0
		
		repeat 
			task.wait() 
			z += 0.03
			warn(z)
		until loaded.Length > 0  or z >= 20
		
		if loaded.Length > 0 then
			warn('Got length',times.Length) -- say the length
			times.Length = loaded.Length -- set length

			loaded.KeyframeReached:Connect(function(keyframe)
				print(keyframe)
			end)

			loaded:Play()
			loaded.Stopped:Wait()
			loaded:Destroy()
			rig:Destroy()
			warn(times)
		else
			warn('COULD NOT GET LENGTH')
		end

I have a similar issue with my animations. For some reason the length of an animation only exists when it’s played. Try firing loaded:Play() then getting the animation length to see if that works.

Hmm same thing

	if anim and anim:IsA('Animation') then
		local rig = script.Parent:WaitForChild('Rig',30):Clone()
		local animator = rig:FindFirstChild('Humanoid'):FindFirstChild('Animator')
		rig.Parent = workspace
		
		warn('FETCHING TIMES')
		local times = {
			Length = 0,
		}
		
		local loaded = animator:LoadAnimation(anim)
		
		local z = 0
		
		loaded:Play() -- play it
		
		repeat 
			task.wait() 
			z += 0.03
			warn(z)
		until loaded.Length > 0  or z >= 20
		
		if loaded.Length > 0 then
			warn('Got length',times.Length) -- say the length
			times.Length = loaded.Length -- set length

			loaded.KeyframeReached:Connect(function(keyframe)
				print(keyframe)
			end)

			
			loaded.Stopped:Wait()
			loaded:Destroy()
			rig:Destroy()
			warn(times)
		else
			warn('COULD NOT GET LENGTH')
		end
	end
1 Like

You’re simply not going to get that number very quickly. According to the docs:

[Length is] 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.

Try pre-loading it before reading the property.

1 Like

ahh ok yeah that’d make sense. Question, how do I preload it?

2 Likes

ContentProviderService is your friend

1 Like

Ahh cool, I also sadly dont know how this works, I tried reading through the doc but Im not exactly sure what I should use to do this, so if you could answer this that would be nice! Thanks!

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