I would like to make my custom character walk with an animation I have made.
When I try the script I have made, it doesn’t work.
What I think the issue is:
I believe my script works, but another script is getting in the way of mine. Roblox Studio automatically makes a LocalScript on my custom character when it spawns called Animate. This Animate script checks for many parts my custom character does not have, such as a “right shoulder” and a “neck”.
What I have done so far to solve my issue:
I have looked on the DevHub and followed 2 tutorials, I looked on YouTube and followed a custom character video from GnomeCode, I have looked on the DevForum and followed others code, and I have tried solving the issue multiple times by myself with my knowledge alone.
My Script Here: ( I have changed it multiple times and no results yet and no errors for this script )
local humanoid = script.Parent.Humanoid
local walkAnimationID = script.WalkAnimation
while true do
wait()
if humanoid.WalkSpeed > 0 then
walkAnimationID:Play()
end
end
Here is an image of the tree:
Image of my Character:
Errors that happen when I run the game:
Is your code inside the animate script? Also you keep playing the walk animation, you should first check if it is already playing.
Correct code [use if you can’t do it on your own]:
local humanoid = script.Parent.Humanoid
local walkAnimationID = script.WalkAnimation
walkAnimationID = humanoid:LoadAnimation(walkAnimationID)
while true do
wait()
if humanoid.WalkSpeed > 0 then
if walkAnimationID.IsPlaying == false then
walkAnimationID:Play()
end
end
end
EDIT: Also noticed you didn’t load the animation on the humanoid so I added it for you.
I have switched the code around to the one you have given, yet the animation still doesn’t play.
My code is inside of the starter character and the local script is called “MyAnimate”.
In the main post I gave a tree, but I will post it here to clarify:
I have recorded what happens when I run the game, but I had to edit it so that my save file info wasn’t leaked. Also, I have never uploaded a video to the DevForum before so I don’t know if it works.
To play an animation, you first need to make an animation track using :LoadAnimation().
Also, you should not use a while loop, as it will constantly set your animation to it’s starting position.
Try this instead:
local rs = game:GetService("RunService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local walkAnimation = humanoid:LoadAnimation(script.WalkAnimation)
rs.RenderStepped:Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
if not walkAnimation.IsPlaying then
walkAnimation:Play()
end
else
walkAnimation:Stop()
end
end)
This will only play the animation if it isn’t already playing when the character is moving.
The custom character now works, thanks for your help @goldenguy9 and @DEVLocalPlayer! I did what you said and here is the output result: ( also the animation continuously plays, but I can fix that later )
Thanks for telling me about this btw. Although my original solution was solved, this helped too. I currently am still learning scripting and I don’t really know how to use the RunService yet. I will make the code to fit what I currently understand, but change it later when I understand the RunService.