Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Can I Detect If My Player Is Running? [Solved]

Asked by
fff054 51
5 years ago
Edited 5 years ago

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!

0
That is not a valid reason to downvote this kind of question. In this case, he clearly stated what he tried. Instead of being toxic, actually figure out a solution to the problem. User#29813 0 — 5y
0
Doesn't matter. There needs to be code anyways. it's possible OP executed his plan incorrectly. Therefore my downvote still remains valid since there is no code. Mind clicking the link that explains in depth. You see, this question is only useful to the asker since the code is not available for us to see, and anyone in the future who looks up this question would have to guess what it is exactly. programmerHere 371 — 5y
0
I read from the link before I even posted my comment complaining about the downvote. I still believe the downvote was unjustified. The issue was clearly stated, and code would have probably confused, rather than enlightened, other readers. User#29813 0 — 5y
0
And how so. Again the question is only useful to the asker, not future readers. You can't really justify that. My downvote is still valid. Period. programmerHere 371 — 5y

2 answers

Log in to vote
0
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:

01local player = game:GetService("Players").LocalPlayer
02local character = player.Character or player.CharacterAdded:Wait()
03local humanoid = character:WaitForChild("Humanoid")
04local is_running = false
05 
06humanoid.Running:Connect(function(speed)
07    if speed > 0 then
08        is_running = true
09    else
10        is_running = false
11    end
12end)
13 
14while true do
15    print(is_running)
16    wait(3)
17end

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:

01local player = game:GetService("Players").LocalPlayer
02local character = player.Character or player.CharacterAdded:Wait()
03local humanoid = character:WaitForChild("Humanoid")
04local hrp = character:WaitForChild("HumanoidRootPart")
05local is_running = false
06local y_out_vector = Vector3.new(1, 0, 1)
07local last_position = hrp.Position*y_out_vector
08 
09humanoid.Running:Connect(function()
10    local current_position = hrp.Position*y_out_vector
11    if current_position ~= last_position then
12        is_running = true
13    else
14        is_running = false
15    end
View all 22 lines...

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!

0
You can simplify to `is_running = speed > 0` and `is_running = current_position != last_position` programmerHere 371 — 5y
0
Thanks Too Much! I Wish You The Best fff054 51 — 5y
0
Sometimes reduction removes the ability to understand for beginning users. I prefer the lengthier version for its simplicity and comprehensibility. The quality of code is not determined by its length or the cool syntactical tricks used within. User#29813 0 — 5y
0
Ofc not but thinking smarter and not harder is good too programmerHere 371 — 5y
View all comments (3 more)
0
That comment was explicitly inorrect. If anything, beginners have to think harder for your solution. Since I'm writing an answer that should be understood by anyone, I'm going to use the easiest to understand methods. User#29813 0 — 5y
0
Would someone mind explaining why I was downvoted? I am always working to improve my methods, so I'd appreicate quality criticism. User#29813 0 — 5y
0
Im pretty sure people understand how to use relational operators so using `a = b == c` is still readable (ps wasnt me who downvoted, i only downvoted question, i upvoted ur answer) programmerHere 371 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

01local character = player.Character or player.CharacterAdded:Wait()
02local humanoid = character.Humanoid
03 
04while true do
05    wait()
06    if humanoid.MoveDirection ~= Vector3.new(0,0,0) then
07        -- Running
08    else
09        -- Not running
10    end
11end

Answer this question