Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

2 comments

0

UserInputService and ContextActionService provide you with gamepad support. Any other reason to not use them?

0

I don't really like infinite loops, but I think my updated solution will work for your purpose.

1 answer

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

6 comments

0

This is a good local way, but unfortunately, this is within a server script :(

0

That is not how RenderStepped works or should be used for.

0

Just because something wasn't made for a purpose, if it works, and it works well, then you can use it.

0

no

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.