How to play animation in runtime?

I uploaded my mesh plus animation as an .fbx file.
I am able to load my animation and play my animation with the Animation Editor, please see the screenshot below:

Question:

  • How to play that animation in a loop, while playing the game?

thanks for any help,
appreciate it!

I don’t use animations very often but this should work:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://(upload your animation and put the asset ID here)"

local animator = Instance.new("Animator")
local animationTrack = animator:LoadAnimation(animation)

-- Start playing the animation
animationTrack:Play()
-- Play the animation again every time it finishes
animationTrack.Ended:Connect(function()
    animationTrack:Play()
end)

It should work if you put the code in a server script inside your model. Honestly I’ve never used the new Animator instance (I always just use the Humanoid API), so you’ll need to double check if that works properly. The code should work regardless of what you’ve set the animation’s looped property to in the animation editor.

Thank you so much.

I added your script to my mesh, but unfortunately, it doesn’t work.
And here is my asset id: rbxassetid://16386213437

I attached the .rbxl file for you to download:
my_character.rbxl (1.1 MB)

Hmm… not sure what I am missing here.

About the Humanoid API:
How would you setup this character that way as well?

Thank you so much for your help,
super cool! :hugs:

1 Like

When making the animation there is a setting to press “loop animation”. Edit your animation and press loop then save again.

1 Like

@KRYPTIC010 thank you so much for your help.

I guess you are referring to the Animation Editor.
Well, when I saved my animation I had the “Loop” turned on.

Please see the screenshot below.

I saved again over the existing animation with the “Loop” activated.
But the issue is still the same. The Character Animation doesn’t even begin to play, when in “Play Mode”.

Sorry for the late reply, I missed the notification.

I don’t know why I made this so unclear, but the animator needs to be the one your character uses, not just some random one I created for some reason.

Here’s some code but with the path to your Animator from the explorer picture.

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://(upload your animation and put the asset ID here)"

-- This should be the correct path to your Animator
local animator = script.Parent.Parent.AnimationController.Animator

local animationTrack = animator:LoadAnimation(animation)

-- Start playing the animation
animationTrack:Play()
-- Play the animation again every time it finishes
animationTrack.Ended:Connect(function()
    animationTrack:Play()
end)

Maybe see if that code works instead.

Thank you so much. This is great progress, it plays the animation on a static X,Y,Z location. Basically, it seems the location is fixed.

Is there a way to play the animation like it’s “original”? Considering also the X,Y,Z world position?
If that’s what it is, I can’t tell.

But if I play the animation right now, the character stays at the same location, while trying to do the animation.

Would be great if the animation could be also played like the original one, in which the character runs around and changes it’s position.

But not sure how to change that?

thank you so much for all your help,
this is amazing!!! :hugs:

1 Like

so I guess, that’s not possible?

I do have an update, please see below attached my .rbxl file for download with the script:

I created a button, when clicked in Play Mode the animation starts.

However, for some reason… there is still the issue, that the character doesn’t move “freely” in space. It looks like the root bone is stuck at a location, or can’t move.

I am running out of any ideas how to get this done.

You can download the .rbxl file with the script right here:
my_animation_test.rbxl (1.1 MB)

Appreciate any little help or idea!