How to check if humanoid is walking?

I know this sounds kinda dumb or something since it is probably easy to find out, but how would I find when the humanoid is walking? I used if Humanoid.MoveDirection.Magnitude > 0 but it was unreliable and when I turned a different direction, the code below the if statement starts again.

Sooo, how would I do this?

2 Likes

use humanoid.Running

humanoid.Running:Connect(function(speed)
    if speed > 0 then
        print("yes")
    end
end)
5 Likes

Umm, I tried it, and It was a little glitchy. Is that the only reliable source, or is there a more reliable one.

1 Like

What about it is glitchy? I use it and it works fine.

Well, can you try an else statement on it? That’s basically what I did. Sorry, should have said that at first.

Yes, you can use an else block.

1 Like

Well, when I tried it, it repeated the code inside the if statement several times. Should I like use some sort of debounce on it?

If you are wanting the code inside the if statement to only run once when the player runs then yes you can use a debounce:

local Debounce = false 
humanoid.Running:Connect(function(speed)
    if speed > 0 and not Debounce then
         Debounce = true
             ----code
       else 
         Debounce = false
    end
end)

Btw, if you wanted to “use the event in an if statement” elsewhere easily, than inside the event, you can create a function to return whether or not the humanoid is walking, something like this

local function IsWalkingListner(Humanoid)
local Walking = false
Humanoid.Running:Connect(function(speed)
    if speed > 0 then
       Walking = true
		else
	  Walking= false
    end
 end)
  return function()
	return Walking
  end
end

Example:

local Player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local IsWalking = IsWalkingListner(Humanoid)

while true do
	wait()
	if IsWalking() then
        print("Walking!")
     end
end

3 Likes

Another method would be checking the Humanoid’s MoveDirection property, here’s an example:

if Humanoid.MoveDirection.Magnitude > 0 then
3 Likes

I already tried that. Thanks for the suggestion though!

Ooh, my bad, I didn’t see that. I’m really tired right now. I don’t understand why that wouldn’t work though, any errors? And if you don’t mind, can you share your code?

1 Like

You could perhaps try to see if the players running sound IsPlaying. Not guaranteed to work, just a random idea.

What about using StateChanged

humanoid.StateChanged:Connect(function(state)
     if state == Enum.HumanoidStateType.Running then
     
     end
end)
5 Likes

Increase the threshold to 0.3 and remove the Y component. I used that solution, it works fine.

It says in https://developer.roblox.com/, that it only fires after you jump or something like that. I tried it, as expected, the code in the statement is started after I jump.

1 Like

(vel * Vector3.new(1, 0, 1)).Magnitude > threshold
or
sqrt(vel.X*vel.X + vel.Z*vel.Z) > threshold
use
vel = rootPart.Velocity - standingPart.Velocity if you want to account for any platforms they are currently standing on somewhat.
Otherwise, just vel = rootPart.Velocity should be good enough to detect if they are moving at all.
You can also check their Humanoid.MoveDirection which resets to <0,0,0> when there is no input from controllers / keyboard / etc.
All these are viable ways of detecting if they are walking or moving at all.

3 Likes

RunningNoPhysics is often called some time after a jump, when on a flat surface, or before a jump too.

1 Like