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!
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!
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.
I am getting this error: 13:55:33.671 - Workspace.Sinister.Script:5: attempt to index local ācontrollerā (a nil value)
Then you didnāt set controller correctly. Set it correctly and it will work.
Yeah, I just figured that out, I fixed that but now nothing is popping in the output and animation is not working.
What is your current code?
What do you mean? The ID?
No, what code is currently inside your script?
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://2787466343"
local controller = workspace.Sinister.Humanoid ā path to Humanoid/AnimationController
controller:LoadAnimation(anim):Play()
Hm⦠Nothing is in the output, correct?
Yes, correct. Completely empty.
Can I see the explorer? So I can see the path?
as you see in workspace and in the model it is.
It is a group animation because I am creating a game inside the group? Should it be normal animation or group animation?
In output it just says that place saved.
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ā¦
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
NOTE: this will play the animation over and over again.
@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.
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.
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!
This is an interesting way to run a piece of code only once.
Consider leaving out the ābreakā if you donāt want the while statement to be redundant.