How do I loop animations?

Greetings again,

I am trying to make a script that loops an idle animation on an NPC, it works in Testing Mode but not on public servers. Here is the code:

local animation = script:WaitForChild("Animation")
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid.Animator
local loadAnim = animator:LoadAnimation(animation)

while true do
	task.wait(2.9)
	loadAnim:Play()
end

1 Like

There’s a Looped property of AnimationTrack.

animationTrack.Looped = true

Make sure the animation is actually owned by you or the group that published the experience.

1 Like

I tried it earlier and still it wouldn’t work, and yes the animation is by me.

I don’t see a particular reason for the animation not to play. Enabling ‘looped’ and playing the animation is the best way, and as long as animations are not overriding each other (animation priority), and they are owned by the publisher, there should be no issues. Are you surely testing on the lastest version of your place? Any error messages in the console?

There is no errors outputted, it only plays the animation once and that’s it.

Try republishing the animation with the looped property enabled in the animation editor.

1 Like

@Mortai_Yt is right, you can re-publish the animation in a loop by default.

There seems to be an issue with how .Looped is replicated across the client-server boundary, originating in the year 2018. (AnimationTrack.Looped Does Not Replicate)

Nonetheless, I haven’t been able to reproduce the issue with your code.

local animation = script.Animation
local humanoid = script.Parent.Humanoid
local animator = humanoid.Animator
local loadAnim = animator:LoadAnimation(animation)

while true do
	loadAnim:Play(); loadAnim.Ended:Wait()
end

Instead of experimenting with playing the animation by a script, it makes more sense to handle visuals locally, just like GUIs. A local script in StarterPlayerScripts looping an animation worked flawlessly for me.

local animation = workspace.NPC.Animation
local humanoid = workspace.NPC.Humanoid
local animator = humanoid.Animator
local animTrack = animator:LoadAnimation(animation)
animTrack.Looped = true
animTrack:Play()

Within the animator plugin, there’s an option to loop the animation. There is no need for code on this matter…

There is scenarios where it would be less effort to do this for example using Default Roblox Animations such as emotes where there is no loop, it is more effective to just tick Looped than getting the keyframesequence

1 Like

extremely loud incorrect buzzer