How can I script a NPC to do an animation?

Hello, so I made an animation and saved it in my group, I was wondering how can I script a NPC to do the animation? If you know this and you are a good scripter, please reply!

Thanks!

14 Likes

Put a Humanoid or AnimationController inside the NPC, then:

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://PUT_ID_HERE"

local controller = ... -- path to Humanoid/AnimationController
controller:LoadAnimation(anim):Play()

You can get the ID from the link of the animation. If your link is i.e.:

https://www.roblox.com/library/12345/Some-Animation

Then your ID is 12345.

34 Likes

I am getting this error: 13:55:33.671 - Workspace.Sinister.Script:5: attempt to index local ā€˜controllerā€™ (a nil value)

3 Likes

Then you didnā€™t set controller correctly. Set it correctly and it will work.

2 Likes

Yeah, I just figured that out, I fixed that but now nothing is popping in the output and animation is not working.

3 Likes

What is your current code?

2 Likes

What do you mean? The ID?

1 Like

No, what code is currently inside your script?

1 Like

local anim = Instance.new("Animation")

anim.AnimationId = "rbxassetid://2787466343"

local controller = workspace.Sinister.Humanoid ā€“ path to Humanoid/AnimationController

controller:LoadAnimation(anim):Play()

2 Likes

Hmā€¦ Nothing is in the output, correct?

2 Likes

Yes, correct. Completely empty.

2 Likes

Can I see the explorer? So I can see the path?

1 Like

image as you see in workspace and in the model it is.

1 Like

It is a group animation because I am creating a game inside the group? Should it be normal animation or group animation?

1 Like

In output it just says that place saved.

1 Like

Group Animations must be published under Group games only, same with personal places/animations. Iā€™m not sure if this would help in any way, but possibly try setting the controller to script.Parent.Humanoid, and unanchoring the entire model. (Not sure if this will work, but it seems logical?) I also cant really test as I dont animateā€¦

3 Likes

You could do:

while true do
	local hum = script.Parent:WaitForChild("Humanoid")
		local anim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
	anim:Play()
	wait(5)
	break
end
  • Just make sure that the animation is inside the script and that the script is inside the character.

NOTE: this will play the animation over and over again.

2 Likes

@XxKinqSinisterxX Hereā€™s your solution, what buildthomas said & you have to own the animation, put the script in

wait(3)
local hum = script.Parent.Humanoid

local anim = hum:LoadAnimation(script.Parent.Animation)

anim:Play()

Knowing you have the animation inside.

3 Likes

Instead of using a loop, he can make that animation looped. If itā€™s not looped, then he can program it like this.
anim.Looped = true

However, if your game is in the group, you must publish your animation to that group. Otherwise it wonā€™t work.

EDIT: You can place an animation with AnimationId of the animation you made for that NPC. Like this.
Untitled
Once you do that, you can do the following code.

local hum = script.Parent:WaitForChild("Humanoid") -- Find the Humanoid
local anim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation")) -- It finds anything inside of the script that is "Animation" and loads it.
anim.Looped = true -- If your animation is not looped, you can loop it like this.
anim:Play() -- At the end you play the animation

I hope this was helpful!

8 Likes

This is an interesting way to run a piece of code only once. :slight_smile:

Consider leaving out the ā€œbreakā€ if you donā€™t want the while statement to be redundant.

7 Likes