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.
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.
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”
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.
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
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.
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))