How can I make idle animation stop when I move?

So I’ve got code working to make the script work, when you press W, A, S, or D, the animation will stop and you’ll do the normal R6 walking animation, and I make it so that when the input ends, the idle animation plays again. BUT, if you’re pressing, say W, then press D at the same time to go to the right and stop pressing W, the idle animation plays. How can I make it so that it only plays if NONE of the W, A, S, or D keys are being pressed down?
Here’s the code:

local tool = script.Parent

local Animation = script:WaitForChild("Idle")

local userInput = game:GetService("UserInputService")

local stopAnimKeys = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D}


tool.Equipped:Connect(function()
  local Char = tool.Parent

 	if Char then

	local Humanoid = Char:WaitForChild("Humanoid")

	if Humanoid then



		if Humanoid.RigType == Enum.HumanoidRigType.R6 and Animation then

			LoadedAnim = Humanoid:LoadAnimation(Animation)

		end

		if LoadedAnim then

			LoadedAnim:Play()

		end


	end
  end
end)


local Char = tool.Parent

userInput.InputBegan:Connect(function(input, processed)
if processed then return end
if table.find(stopAnimKeys, input.KeyCode) then
	LoadedAnim:Stop()
  end
end)

userInput.InputEnded:Connect(function(input)
    LoadedAnim:Play()
end)

Help!!

You can detect whenever the player’s character’s Humanoid’s MoveDirection property changes, and when it does: if it’s not equals to Vector3.new(0, 0, 0) then stop the animation

Ok, thanks! Could you show me a little bit of sample code to see how to do that?

local humanoid = character:WaitForChild("Humanoid")

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function ()
if humanoid.MoveDirection ~= Vector3.new(0,0,0) then
--stop anim
end
end)

Excuse me for any missing brackets or spelling mistakes, I am on phone.

2 Likes

I’m not sure why, but this:

h:GetPropertyChangedSignal("MoveDirection"):Connect(function()
    	if h.MoveDirection ~= Vector3.new(0,0,0) then
	    LoadedAnim:Stop()
    end
end)

isn’t working for some reason. I have LoadedAnim identified, as you can see in my post, but the animation doesn’t stop when I move.
Typed out print(“Hello world”) and nothing printed, so I know that isn’t the problem

Can you confirm that it’s detecting change? And if it’s entering the if statement? Also something that could be troublesome is you have an event listener thet plays the loading animation every time someone stops pressing any button

Delete this

I did before I tested the new code :confused:

Then we test if code is detecting change of move direction. Prints!!

1 Like

I have to go offline for now, but I’ll work on this later. Thanks for the help! I didn’t even think about detecting if the character is moving, to play the idle animation.

No worries, I’ll do the same and will continue helping you next time.

Alright, I’m back online. Not quite sure how to do this, once you get back online could I get some help?

A really easy fix to this is just to use the roblox’s Animate script. You can get this by pressing play and then going to the explorer and entering your player, then search for the animate script and copy it. I believe you put it into starterchracterscripts after that you can just change the id’s of the animations and viola you have custom running walking idle jumping etc animations without having to do any work at al…

Only thing is, what if I want different animations for different tools?

1 Like

Like change the tools walking animation?

local tool = script.Parent

tool.Equipped:Connect(function()
	humanoid = tool.Parent:WaitForChild("Humanoid")
	description = humanoid:GetAppliedDescription()
	local descriptionClone = description:Clone()
	descriptionClone.IdleAnimation = --Put your ID
	descriptionClone.WalkAnimation = --Put your ID
	humanoid:ApplyDescription(descriptionClone)
end)

tool.Unequipped:Connect(function()
	if humanoid and description then
		humanoid:ApplyDescription(description)
	end
end)
1 Like