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

Is there a more accurate way to detect whether the player is moving or not?

Asked by 5 years ago

Roblox generously provides a code to detect whether a player moves or not:

game.Workspace.Player.Humanoid.Running:Connect(function(speed)
    if speed > 0 then
        print("Player is running")
    else
        print("Player has stopped")
    end
end)

but the problem with this is that the script sometimes misfires, meaning the when the script prints "Player is running", it would fire 3 times or so before stopping, and the same would be when the player stops.

Is there an accurate way to outright detect whether the player began moving or completely stop. I preferably would not like to use key pressing events as I would like this to work on Console where they use thumbsticks.

0
UserInputService and ContextActionService provide you with gamepad support. Any other reason to not use them? xPolarium 1388 — 5y
0
I don't really like infinite loops, but I think my updated solution will work for your purpose. SteamG00B 1633 — 5y

1 answer

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
5 years ago
Edited 5 years ago

I don't know if this is more accurate or not, but it is a different way of doing this. I'm also going to assume you have placed this script inside character scripts.

if script.Parent.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
    --do stuff
end

So you would place this inside character scripts as a local script, and it definitely is a simpler solution than roblox's function there.

Edit: If you have this inside of RenderStepped, it will be more accurate because it will be checking every tick for movement.

game:GetService("RunService").RenderStepped:Connect(function()
    if script.Parent.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
            print(script.Parent.Humanoid.MoveDirection)
    end
end)

So I don't know how you'll like this solution, but it should work for your purpose

local previous = Vector3.new(0,0,0)
while true do
    if game.Workspace.Player.HumanoidRootPart.Position~=previous then
        print("Player is running")
        previous = game.Workspace.Player.HumanoidRootPart.Position
    else
        print("Player has stopped")
        previous = game.Workspace.Player.HumanoidRootPart.Position
    end
end
0
This is a good local way, but unfortunately, this is within a server script :( Marmalados 193 — 5y
0
That is not how RenderStepped works or should be used for. xPolarium 1388 — 5y
0
Just because something wasn't made for a purpose, if it works, and it works well, then you can use it. SteamG00B 1633 — 5y
0
no User#23365 30 — 5y
View all comments (2 more)
0
But Roblox maxes out at 60 frames per second, and it's almost impossible to lose frames on roblox with any computer this day and age, so you'd get a constant update speed of 1 update per 1/60th of a second. SteamG00B 1633 — 5y
0
The only reason why people don't like fallout's game engine to use this method is because not everyone can run fallout at max fps, but roblox is so much less resource intensive, so it would be fine. SteamG00B 1633 — 5y
Ad

Answer this question