Need help with NPC Animate script

Hi! So, I need help with my Animate ServerScript for an NPC I’m making.
The thing is, The animation Plays, but, It only plays once.

Here is my Script:
(Note that I’m using CUSTOM Animations)


local humanoid = script.Parent.Humanoid

local walkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)

humanoid.Running:Connect(function (speed)
	if speed > 0 then
		if not walkAnimTrack.IsPlaying then
			walkAnimTrack:Play()
		end
	else
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end
	end
end)

Character is R6, and I can’t seem to figure out why It’s not working correctly.
Also, I followed "GnomeCode"s Custom Character’s tutorial and got the script from there.

2 Likes

where is that script ,starterCharacterScripts?starterPlayerScripts?
StarterPack?

1 Like

Inside of the NPC, Not in any of those.

1 Like

Is the animation looped? The event doesnt fire so often, so it is not completely in sync with your animation, leading to it needing to be looped to help with fluidity.

it fires every time you hit a movement key once))

Its not for a player…
Its for a NPC, as I stated in the Title and description.
If it was for a Player, Then I would’ve stated it was a “Local Script”, and not a “ServerScript”

1 Like

I was stating how often the event fires regardless, but apologies for not stating your case.

is the humanoid ever going to walk?

1 Like

Obviously, Yes. I kept out the

humanoid:Move(Vector3.new(10,0,10))

Just because I wanted to include the Animation script.

1 Like

Is the animation looped, still?

The event should fire while the NPC is moving. By that I mean if the NPC is moving, The animation is Playing.

1 Like

if the event fires once then the function is called once
try let the npc move more than once

1 Like

How do Ioop It-? I don’t see a “Loop” property anywhere…

1 Like

Its consistently moves. Like, The NPC moves Until it falls off the edge and dies.

1 Like

I believe you can edit the animation track and make it looped like this;
AnimationTrack.Looped = true
And also while you are making the animation in the Animation Editor by pressing a button that looks like a play button and restart button combined. Saving the animation with that button toggled will permanently make the animation looped.

1 Like

I’ll try that, Thanks.

Also, I do know about that “loop” button in Animation Editor.

1 Like

then try

function myFunction(speed)
	if speed ~= Vector3.new(0,0,0)then
		if not walkAnimTrack.IsPlaying then
			walkAnimTrack:Play()
		end
	else
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end
	end
end
while wait() do
    myFunction(humanoid.MoveDirection)
end
1 Like

Note that there may be an issue with stopping manually looped animations (going into anim editor and looping) and instead you may want to try doing it via code.

1 Like

May i ask if ncall means anything specific in that block of code?

1 Like

That worked. You have my eternal Gratitude. Thanks for helping me out!

For Reference, this is the Finished Script:

local testgoal = game.Workspace.testpart


local humanoid = script.Parent.Humanoid

local walkAnim = script.Walk
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)

humanoid.Running:Connect(function (speed)
	if speed > 0 then
		if not walkAnimTrack.IsPlaying then
			walkAnimTrack:Play()
			walkAnimTrack.Looped = true
		end
	else
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end
	end
end)
--Below this line is the Testing Code
humanoid:MoveTo(Vector3.new(testgoal.Position.X, testgoal.Position.Y, testgoal.Position.Z))
1 Like