Answered by
5 years ago Edited 5 years ago
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:
- It fires when the speed of the player changes.
- It does not fire repeatedly while the player is moving at a consistent speed.
- It fires when the player jumps straight up and down.
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:
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local character = player.Character or player.CharacterAdded:Wait() |
03 | local humanoid = character:WaitForChild( "Humanoid" ) |
04 | local is_running = false |
06 | humanoid.Running:Connect( function (speed) |
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:
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local character = player.Character or player.CharacterAdded:Wait() |
03 | local humanoid = character:WaitForChild( "Humanoid" ) |
04 | local hrp = character:WaitForChild( "HumanoidRootPart" ) |
05 | local is_running = false |
06 | local y_out_vector = Vector 3. new( 1 , 0 , 1 ) |
07 | local last_position = hrp.Position*y_out_vector |
09 | humanoid.Running:Connect( function () |
10 | local current_position = hrp.Position*y_out_vector |
11 | if current_position ~ = last_position then |
16 | last_position = current_position |
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.
I hope this helps. Have a great day scripting!