Scripts says that the animation track's length is 0, while its a ctually not

Hey, guys
I was making a pvp module between 2 players, and the cooldown between their attacks, I’m using animation length.

Firstly I used animationTrack.Ended:Wait(), and it worked well, but I remembered about the fact that the script will fall if 1 of the player will leave, so I did something and tested; if the player what carries cooldown with their animations leave, then the script will wait forever and never continue.

So I thought about animationTrack.Length, why not just doing something like:

task.wait(animationTrack.Length)

It didn’t worked. The whole pvp freezed without an error or warning message.
So I wrote:

local sunrise_start_animation = sunrise_hum.Animator:LoadAnimation(animations_folder.start.sunrise)
	local sunrise_idle_animation = sunrise_hum.Animator:LoadAnimation(animations_folder.idle.sunrise)
	local sunrise_attack_win_animation = sunrise_hum.Animator:LoadAnimation(animations_folder["something about sunrise"]["sunrise hit → win"].sunrise)
	local sunrise_attack_fail_animation = sunrise_hum.Animator:LoadAnimation(animations_folder["something about sunrise"]["sunrise hit → fail"].sunrise)
	local sunrise_got_hit_animation = sunrise_hum.Animator:LoadAnimation(animations_folder["something about evgeniy"]["evgeniy hit → win"].sunrise)
	local sunrise_dodged_animation = sunrise_hum.Animator:LoadAnimation(animations_folder["something about evgeniy"]["evgeniy hit → fail"].sunrise)
	local sunrise_double_attack_animation = sunrise_hum.Animator:LoadAnimation(animations_folder["both is attacking"].sunrise)
	local sunrise_double_dodging_animation = sunrise_hum.Animator:LoadAnimation(animations_folder["both is dodging"].sunrise)
	-- animation durations for task.wait() s
	local SSA_Length = sunrise_start_animation.Length
	local SIA_Length = sunrise_idle_animation.Length
	local SAWA_Length = sunrise_attack_win_animation.Length
	local SAFA_Length = sunrise_attack_fail_animation.Length
	local SGHA_Length = sunrise_got_hit_animation.Length
	local SDA_Length = sunrise_dodged_animation.Length
	local SDAA_Length = sunrise_double_attack_animation.Length
	local SDDA_Length = sunrise_double_dodging_animation.Length
	warn(SSA_Length)
	warn(SIA_Length)
	warn(SAWA_Length)
	warn(SAFA_Length)
	warn(SGHA_Length)
	warn(SDAA_Length)
	warn(SDAA_Length)
	warn(SDDA_Length)

Look at the all these warns, they all showing 0. Why??

I tried this script in the command bar:

local animation = Instance.new("Animation", workspace)
animation.AnimationId = "animation here"
local dance = game.Players.LocalPlayer.Character.Humanoid.Animator:LoadAnimation(animation)
print(dance.Length)
dance:Destroy()
animation:Destroy()

and it showed real numbers.
So I’m wondering, how does that works, and where did I failed the job? Why is it showing 0 in the module, but in the command bar it shows real length?

Please, help

1 Like

I believe you may have to first play the animation in order to actually get the length (for runtime anyway).

if that is not ideal for you (or if it doesn’t work), a discrete async method can be established;

local Provider = game:GetService("KeyframeSequenceProvider")

local Cache = {}
local AnimationKeyframeAsync = {}


function AnimationKeyframeAsync.GetAnimationLength(AssetId)
	if Cache[AssetId] then
		return Cache[AssetId]
	end

	local Sequence = Provider:GetKeyframeSequenceAsync(AssetId)
	local Keyframes = Sequence:GetKeyframes()

	local Length = Keyframes[#Keyframes].Time

	Cache[AssetId] = Length

	return Length
end


return AnimationKeyframeAsync

3 Likes

Maybe works, but I found the solution myself. Thank you

(By the way: you don’t need to play an animation to see its length, you only need to load it to animator)

Well, I found the solution.

Its kinda simple: If you try to see the length of an animation on CLIENT side, the script will see it, while SERVER side cannot see it and will always output 0.

My problem happened because my module script is running on the server side, while I tested command bar on the client side.

To prevent these questions as mine, always check your problems on both sides if they can be seen on it either