Animations not playing at the specified time

Hi everyone. I’m having a very annoying issue right now. I’m trying to make an animation play whenever my NPC gets to a certain point. However, it doesn’t work correctly. Got anything that could help?

  1. What do you want to achieve? Animation plays at the specified time.

  2. What is the issue? Animation plays too early. See picture:
    image
    The green part is where the NPC starts. The red part isn’t important. The yellow part is where the animation should begin to play by the time the NPC touches it.

  3. What solutions have you tried so far? Changing the time, still plays too early.

Not exactly sure what to do here. Please help. Here’s the code I am using.

local Knight = script.Parent
Barbarian = nil
Finish = nil
Humanoid = nil
Enemy = nil
Animator = nil
EnemyAnimator = nil

if Knight ~= nil and Knight:FindFirstChild("Humanoid") and Knight:FindFirstChild("PathEnd") then
	Humanoid = Knight.Humanoid
	Finish = Knight.PathEnd
	if Humanoid ~= nil and Finish ~= nil and Knight.Parent:FindFirstChild("Barbarian Staged (RIGHT)") and Humanoid:FindFirstChildOfClass("Animator") then
		Animator = Humanoid.Animator
		Barbarian = Knight.Parent["Barbarian Staged (RIGHT)"]
		Humanoid:MoveTo(Finish.Position)
		if Barbarian:FindFirstChild("HumanoidE") then
			Enemy = Barbarian.HumanoidE
			if Enemy ~= nil and Enemy:FindFirstChildOfClass("Animator") then
				EnemyAnimator = Enemy.Animator
				EnemyAnim = EnemyAnimator:LoadAnimation(Barbarian.Animation)
				EnemyAnim.Priority = Enum.AnimationPriority.Action4
				EnemyAnim:Play()
				task.wait(3.18)
				Anim = Animator:LoadAnimation(Knight.Animation)
				Anim.Priority = Enum.AnimationPriority.Action4
				Anim:Play()
				task.wait(8.24)
			end
		end
	end
end

After 3.18 seconds, that’s when the Knight’s animation plays, which is the amount of time it takes for the NPC to touch the part. However, it plays much earlier than that when the game is run. Any suggestions or tips?

You are handling the animation trigger based on waiting(time).

Seems like you are making a Humanoid:MoveTo(), then Playing an animation, then waiting 3.18 seconds, then playing another animation, then waiting 8.24 seconds again.

I suggest changing your approach and make all those animations and stuff based on Events and not waiting(time).

Example, after using Humanoid:MoveTo() you could wait for the event when the Humanoid reached its goal point with Humanoid.MoveToFinished, in that moment trigger the animation, then wait for the animation to finish with Animation.Stopped:Wait(), then play the next animation and again Animation.Stopped:Wait().

If you need to trigger the second animation while the first one still running, then, use animation Key Events, you can create those from the Studio Animator, its a key that triggers at the middle of an animation, triggers an event, based on that event you can trigger other functions

You need to load it earlier. Loading it after touching the part will cause delays. You should be loading it as soon as you can so that you can play it with no latency.

1 Like