Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How come this NPC animation play script doesn't work? am I missing something?

Hey guys, I'm trying to get my NPC/Models to play animation (automatically) but it doesn't work and I got this error: 14:00:46.507 - Play is not a valid member of Animation 14:00:46.509 - Stack Begin 14:00:46.511 - Script 'Workspace.Worker.Script', Line 4 14:00:46.512 - Stack End

So mainly it's; 14:00:46.507 - Play is not a valid member of Animation

But I don't know what's wrong, I added a script inside the Worker (NPC) so the script's parent is Worker, then I added a new animation into Worker so it's parent is also Worker, now this is the code in the script;

local animation = script.Parent:WaitForChild("Animation")
while wait() do
	wait(0.1)
	animation:Play()
	wait(0.1)
end

As I said, I'm trying to play the animation so it will just keep playing and playing, but it won't,

What's wrong? Should I move something? Do I need a different script?

And lastly, can you help me rewrite the script while explaining or tell me what I did wrong so I can fix it.

Thank you if you really tried to help.

1 answer

✓ Accepted answer

If this is an NPC, make sure you're using a Server Script.

Your problem is you're attempting to use :Play() for an Animation instance. :Play() works for an AnimationTrack, not Animation.


  • What is the Animation Instance?

This is basically an Instance that you give a reference to your animation with. Using the AnimationId property.

  • What is an AnimationTrack?

The AnimationTrack is the actual 'animation' that you can play and manipulate to stop, slow down/speed up, and so on. To create this you must use the :LoadAnimation() function with Humanoid or AnimationController to get the AnimationTrack. Example: local AnimationTrack = Character.Humanoid:LoadAnimation(AnimationInstance)


Here is your fixed script (Assuming you properly made the animation):

local animation = script.Parent:WaitForChild("Animation")
local AnimationTrack = script.Parent:WaitForChild("Humanoid"):LoadAnimation(animation) -- I am assuming you have a Humanoid and script.Parent is the Model of the NPC
while wait() do
    wait(0.1)
    AnimationTrack:Play()
    wait(0.1)
end

Also, why are you using a loop and spamming the Play function for your script? There is a way to edit your animation so it loops normally if that is what you're attempting to do.

This discussion is preserved as it was. New answers, comments, and votes are closed.