How to tell when anim is loaded?

I’ve got a spawning animation for my monster and I have to wait for it to load. As you can see, I’ve got some code that attempts to wait for it to load, but sometimes the animation loads and plays and sometimes it doesn’t play at all. How can I fix this?

Code -

			local exitPortal = humanoid.Animator:LoadAnimation(monster.Animations.ExitPortal)

			while exitPortal.Length == 0 do task.wait() end
			task.wait()
			exitPortal:Play(0)
			wait(0.05)
			monster.HumanoidRootPart.CFrame = assets:FindFirstChild("Map")["Monster Spawn"].CFrame + Vector3.new(0, humanoid.HipHeight, 0)

			exitPortal.Stopped:Wait()

			humanoid.JumpPower = 40
			humanoid.WalkSpeed = walkSpeed

			if monster:FindFirstChild("AI") then
				monster["AI"].Disabled = false
			end

			local color = characterList[monster.OrgName.Value].Color:ToHex()
			module.sendGameEvent([[<font color="#]].. color ..[["><stroke color="#]].. color ..[[">]].. monster.OrgName.Value ..[[</stroke></font> is coming...]])

I think you can check it by this

if animationTrack.Length > 0 then

I have a while loop that checks that already, but it doesn’t seem to be very reliable

Try using Animation:IsLoaded()
It returns a boolean value that tells if the client has finished loading the animation.
Maybe that would help?
I’m not sure if it will work though.

No, that’s for an Animation, not the AnimationTrack (the thing that is returned when you load an animation). Besides, I already tried that :confused:

So, you load the animation onto the humanoid, and then repeat waiting until its length is greater than 0. This works very well. I use it on all my games.

local Anim = humanoid:LoadAnimation(animation)
repeat wait() until Anim.Length > 0

Shouldn’t you load it onto the animator? that’s poor practice using the humanoid instead

Im still new to animating, and i much prefer scripting. I barely look into animations, but loading them onto a humanoid works.
Ive never heard of loading animations onto the animator.

Loading animations onto the humanoid is a deprecated function, so you should use animator:LoadAnimation instead as that still gets updated

Well, time to rewrite an entire 50-ish lines of code on my 1000 line gun script :slight_smile:

Just replace Humanoid:LoadAnimation() with Humanoid.Animator:LoadAnimation() using CTRL + F

To avoid breaking, CTRL F Humanoid:LoadAnimation( and replace with Humanoid.Animator:LoadAnimation(

so the other half doesnt get replaced and cause errors

Just replace them with the find all replace all tool

Okay so, I would prefer using the Track’s length (don’t wrap it in a while loop, use something like repeat) or by waiting the time of the animation to load, which is around 0.5 seconds per animation in ROBLOX.

I’ll try the repeat loop, don’t want to wait 0.5 seconds, because if there’s a delay it won’t play

Would be better to define the ‘Animator’ object instead and then replace each instance of Humanoid:LoadAnimation with Animator:LoadAnimation.

Also use task.wait() instead of wait(), much more efficient and wait() is deprecated too :confused:

I worked on my custom animation player which is here. You might find it useful? you can just play the animation literally once. Might not be most optimized way of doing stuff I have no idea…

Use AnimationTrack:IsPlaying and task.wait and repeat and disconnect stuff

replace

to

local exitPortal = humanoid.Animator:LoadAnimation(monster.Animations.ExitPortal)

local animationLoaded = false

local connection = exitPortal:GetPropertyChangedSignal("IsPlaying"):Connect(function()
	if exitPortal.IsPlaying then
		animationLoaded = true
	end
end)

repeat task.wait() until animationLoaded

connection:Disconnect()
1 Like

That worked! Thanks, here’s the updated code if you wanna see!

		task.spawn(function()
			local exitPortal = humanoid.Animator:LoadAnimation(monster.Animations.ExitPortal)

			monster.Animate.Disabled = true
			for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do
				v:Stop()
			end
			for i, v in pairs(monster.HumanoidRootPart:GetChildren()) do
				if v:IsA("Sound") then
					v:Stop()
				end
			end

			repeat task.wait() until exitPortal.Length > 0
			
			exitPortal:Play(0)
			repeat task.wait() until exitPortal.IsPlaying
			task.wait(0.1)
			monster.HumanoidRootPart.CFrame = assets:FindFirstChild("Map")["Monster Spawn"].CFrame + Vector3.new(0, humanoid.HipHeight, 0)

			exitPortal.Stopped:Wait()

			humanoid.JumpPower = 40
			humanoid.WalkSpeed = walkSpeed
			monster.Animate.Disabled = false
			if monster:FindFirstChild("AI") then
				monster["AI"].Disabled = false
			end

			local color = characterList[monster.OrgName.Value].Color:ToHex()
			module.sendGameEvent([[<font color="#]].. color ..[["><stroke color="#]].. color ..[[">]].. monster.OrgName.Value ..[[</stroke></font> is coming...]])
5 Likes