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

How to make an NPC loop-play an animation?

Hi! I'm a beginner scripter so I don't know how to script well, I tried making this but it doesn't work.

local anim = "AnAnimation"

while true do
anim:Play()
wait (0.99)
wait (0.99)
wait (0.99)
end

Any help would be helpful, thankiew! :3

3 comments

0

Why are you using wait like that?

0

wait(2.97)

0

make the animation Loop

2 answers

Run from a (Server) Script

local animation = script.Parent
animation.AnimationId = "rbxassetid://0"
local model = workspace:WaitForChild("Model")
local animPlayer = model.Humanoid:LoadAnimation(animation)

while true do
	if script.Parent.TurnOff.Value == false then
		animPlayer:Play()
		wait(2)
	else
		wait()
	end
end

Notes

  1. Line 1: Make sure "script.Parent" is an Animation within ServerScriptService

  2. Line 2: Set this to the id number of the animation (you MUST own it) If you set this value on the animation itself in Studio, you can also just remove this line.

  3. Line 3: Set "Model" to the name of the NPC Model

  4. Line 4: Change this location (workspace.Model.Humanoid) to the NPC you want to animate

  5. Line 7: This is a BoolValue named "TurnOff" that when set to true will end the animation, it is parented under the Animation

  6. Line 9: The number in the wait(#) should be the amount of time the animation runs for (default of 2)

Explanation: You cannot just play an Animation, it must be loaded into the NPC and played from an Animation Track, which is automatically made when you load the Animation into the Humanoid

6 comments

1

(He used this for his answer) animation.AnimationId = "rbxassetid://0"

0

i have a question, why not "game.Workspace. ---" instead of "workspace. ----" ?

0

still confused..

1

@BuzzKill, you can write game.Workspace, its just that its quicker to use the single word workspace as its pre-defined by ROBLOX

0

I put the animation ServerScriptService right? and the script's parent is the nimation right? and theres a bool value parented in the animation right?

1

yep, also make sure the bool's name matches the one in the script (animation under ServerScriptService, bool and script both under animation)

Serpentine's answer will work, but there is a more efficient way to do it (in my opinion).

Like he said, you have to load it into a humanoid first and play the AnimationTrack object it creates.

local animation = Instance.new("Animation", script.Parent) -- create a new anim. object
animation.AnimationId = "rbxassetid://507766388" -- roblox idle anim
local model = workspace:WaitForChild("Dummy")
local AT = model.Humanoid:LoadAnimation(animation) -- load anim

while true do
	AT:Play()
	At.Stopped:wait() -- wait for track to stop playing, then play again
end

It utilizes the .Stopped event of the AnimationTrack, which waits for the animation to stop before proceeding.

1 comment

0

at line 08 use AT not At

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