So, I tried a lot of ways to check if my Player was running. I tried with Humanoid.Running and the bad thing about that is that it just fires when your Speed increases or decreases but when you stay at a permanent speed the event doesn't fire. I also tried with Humanoid:GetState() but when you chose Running the If Statement fires even when it is jumping and if I chose RunningNoPhysics nothing happens.
Basically I'm just asking if there's another way or maybe a solution, I would really appreciate if you know the solution and write down how do I make it. Thanks!
There is a simple issue with your logic. The Running
event of the Humanoid
is what you want to use. However, you need to use it in a way that's slightly different than you originally intended. Since I don't know what you plan on doing with the knowledge that the player is running, I'll simply provide some examples of what you can do to know whether or not the player is running. As a quick reference, this
is the wiki page I'll be referring to. Here are the three things we know about this event:
I'm assuming that you wish to avoid the running triggering when the player is simply jumping up and down. I also assume you wish to know that the player is running while they are running at a static rate of change. To do this, let's consider a few important facts. If the player jumps straight up, only the y
value in their positional vector changes, and, if the player stops moving, their speed is zero. Remember that the argument passed to the function connected to the Running
event is the speed at which the player is moving at that time of change. With all this in mind, let's construct a simple setup that allows to, at any time, know if the player is running:
local player = game:GetService("Players").LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humanoid = character:WaitForChild("Humanoid") local is_running = false humanoid.Running:Connect(function(speed) if speed > 0 then is_running = true else is_running = false end end) while true do print(is_running) wait(3) end
As you can see, every three seconds, this outputs whether or not you are running. Therefore, if you're wanting to create a point based walking system (for example), you'd only need to have a basic loop that gives points based on whether or not the is_running
variable is true
. However, this simple system has an issue. It counts jumping straight up as running. While it is true that you can jump forward and be considered running, you're not considered to be running if you're jumping straight up and down. To fix this issue, remember what I said about only the y
value of the positional vector changing. Using this information, we can construct a new system that better fits what running looks like:
local player = game:GetService("Players").LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humanoid = character:WaitForChild("Humanoid") local hrp = character:WaitForChild("HumanoidRootPart") local is_running = false local y_out_vector = Vector3.new(1, 0, 1) local last_position = hrp.Position*y_out_vector humanoid.Running:Connect(function() local current_position = hrp.Position*y_out_vector if current_position ~= last_position then is_running = true else is_running = false end last_position = current_position end) while true do print(is_running) wait(3) end
And you'll notice that, in this new system, we don't even need the speed parameter, since the position evaluation checks whether or not the player moved.
The answer above mine is pretty well explained, but there is another much simpler way to do this...
You can look at the player Humanoid's MoveDirection
which will be some unit vector in the direction the player is walking when they are walking. And it will be Vector(0,0,0)
when they are not walking.
So you would just check to see if the humanoid's MoveDirection
vector is not Vector(0,0,0)
and that will tell you if they are moving or not.
local character = player.Character or player.CharacterAdded:Wait() local humanoid = character.Humanoid while true do wait() if humanoid.MoveDirection ~= Vector3.new(0,0,0) then -- Running else -- Not running end end