There are multiple ways to detect if a character is standing still. The third method is the best since it doesn't consume as much memory as the first and neither needs to check the state of the character to determine if the player is idling as in the second method.
- You could use MoveDirection to check if the humanoid.MoveDirection.Magnitude > 0. If it's higher, it means the humanoid is moving. Example:
local RunService = game:GetService("RunService")
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local function CharIsIdle()
if hum.MoveDirection.Magnitude > 0 then
print("Hum is running")
else
print("Hum is idling")
end
end
RunService.RenderStepped:Connect(CharIsIdle)
*Edit: adding the other method you can use:
- This next method will fire whenever the humanoid starts running. By default, hum.Running sends the speed of the character to the function that connects to it.
So we will check if the speed of the character is higher than 1. If it is higher, then the character is running. If it is lower, then the character is idling. I put it 1 so that it only fires when the character actually has some speed rather than 0 since the character speed for some reason may get higher than 0 but yet is idling.
But since the character can jump or freefall with a speed of 0 (jumping or freefalling in place), we want to get the state of the humanoid to confirm it isn't actually doing either of those first before checking it's speed.
Next, to make sure the scripts only runs once when the character is idling, we add a variable named "isIdling" that is global to the HumIsRunning function but local to the script (this way the variable doesn't reset to false whenever the HumIsRunning function gets called). Now whenever we detect that the humanoid is running or jumping or freefalling we change the variable isIdling to false. And whenever the character is idling, we change the variable to true. Next time if our script confirms that the character is idling, it will check if the variable is not set to true. Only if it's not set to true will it run the subsequent idling scripts. This will prevent the script from running the idling part of the script for more than once unless of course the speed of the character changes higher than 0 then it will run it again next time it idles.
Here's how:
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local isIdling = true
local function HumIsRunning(speed)
local state = hum:GetState()
if (state ~= Enum.HumanoidStateType.Freefall) and (state ~= Enum.HumanoidStateType.Jumping) then
if speed > 1 then
isIdling = false
print("Hum is running")
else
if not isIdling then
isIdling = true
print("Hum is idling")
end
end
end
if (state == Enum.HumanoidStateType.Freefall) or (state == Enum.HumanoidStateType.Jumping) then
isIdling = false
end
end
hum.Running:Connect(HumIsRunning)
Now after everything we've done, if the character is idling it will only run once the subsequent lines and not multiple times like the last method. Although, you could do the same thing with the 1) method and add a boolean variable that changes whenever the char is running or idling so that it runs the script inside either only once.
- The third method is pretty similar to the first but in this case you check the velocity of the HumanoidRootPart of the character. If it's higher than 1 then the humanoid is running/not idling. If it's lower, then it's idling. We will listen to the humanoid state and whenever it get fired, we connect it to our function. This method doesn't need us to check if the character is freefalling or jumping since if they are, their velocity.Magnitude would be higher than 1
Here's how:
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRootPart = char:WaitForChild("HumanoidRootPart")
local isIdling = true
local function GetCharState()
if humRootPart.Velocity.Magnitude >= 1 then
if isIdling then
isIdling = false
print("Character is running")
end
else
if not isIdling then
isIdling = true
print("Character is idling")
end
end
end
hum.Running:Connect(GetCharState)
1 comment
MoveDirection is a Vector3, not a string.