Need help with Custom Character Animation Scripting ( issue solved, Animate local script )

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

Image of my Character:

image


Errors that happen when I run the game:

What the Error is telling me to fix:

I think the solution to my issue is removing Roblox’s Animate script, but how would I do this if it gets added every time I join / run the game?

( I could be completely wrong in what I am saying, but any help would be useful. )

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:
image

That’s really weird, are you getting any errors? It might not find the animation track instance when you’re trying to retrieve it.

The only errors I get are from the Animate script Roblox has, no errors from the script I had or the one you gave.

If you name your animation script “Animate” and put it into startercharacterscripts, it will overwrite the default animation script.

2 Likes

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.

Wait, do something, try adding a print(walkAnimationID.IsPlaying) in the loop to debug and check if the animation is actually playing.

1 Like

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.

1 Like

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 )
image

1 Like

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.

again, thanks for the help. :>