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
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?
@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()
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