How does Roblox move the player?

How does the Roblox Engine move the player? Does it use MoveTo()? I’m currently trying to find the best way to move a player so that I don’t mess up the player movement too much as I’ll be going into moving a live player and I don’t want it to look as if the player has let go of the keyboard and the character is still moving to a specific invisible part/position that I have specified in my script.

2 Likes

Roblox uses UserInputService and ContextActionService to bind player movement to Input Events. You can take a look at the main module here that handles player movement on keyboard:

PlayerMovement.rbxl (21.2 KB)

image

You’ll find what you’re looking for in the ‘Keyboard’ ModuleScript.

You can look into UserInputService and the InputBegan and InputEnded events if you don’t know what they are, (in which case reading those articles would be a good place to start).

3 Likes

I’ll look into it, thanks!

1 Like

Player.Move

What’s the difference between Move() and MoveTo()?

The difference is that :MoveTo() instantly moves an object to a specific position, unless it is used on a Humanoid. If used on a humanoid, it will make the player walk to the position. If the player moves their character (excluding jumping) then the movement will stop. :Move() is also supposed to do generally the same thing. I’ve never seen :Move() in anything but the player modules, specifically in the ClickToMoveController and Invisicam modules.

That’s interesting, but how does Roblox do it? Do they predict the path that the player walks to and set a point like on stud in front for the humanoid to move?

For example, if the player presses W on the keyboard, do they set a point that is in front of the player for the player to move to? And if so, what happens if the player stops pressing W? Wouldn’t the humanoid continue to move forward to that position instead of stopping instantaneously?

Unless Roblox does it such that the point is like 0.001 studs in front of the player so when the player stops, it looks as if he actually just stopped but in actual fact, the player moves a short distance of 0.001 and part therof which is too small to be noticed.

Move causes the character to start or stop walking in a direction specified in the code. For example, the W key would usually be 0,0,-1 with respect to the camera position.

Yep, I understand that but how does Roblox move the player? To simplify this, let’s just say the keyboard. Doesn’t Roblox have to detect for WASD input and move the player respective to the player’s local axis? So what event does roblox use?

That’s all in the script for player control, specifically the one FieryEvent provided.

Detect input with ContextActionService or UserInputService
Move the character with Player:Move(Direction, true)

The true in the function makes the direction become relative to the camera. Roblox handles this internally, as well as the actual physical movement of the player. Those two steps I gave are your only scripted interaction with Roblox character movement.

Here’s a brief rundown of what’s going on with the Humanoid’s movement stuff.


There is a function under the Player object called Move, as mentioned above:

void Player:Move(Vector3 walkDirection, bool relativeToCamera = false)

The Humanoid also has this function:

void Humanoid:Move(Vector3 moveDirection, bool relativeToCamera = false)

In both cases, the Vector3 value you pass to it is a unit vector, which is just a Vector3 value with a length of 1. Unit vectors describe how to move in a certain direction 1 unit.

The Humanoid uses a unit vector to figure out what direction it should be turning towards, and what direction it should apply forces in to move the character in that direction.

The relativeToCamera argument is optional, and tells the Humanoid that its move direction is being described relative to the direction the client’s camera is facing.

Roblox’s default movement script calls Player:Move during every frame, and Player:Move just locates the player’s current humanoid and passes the arguments over to that instead. Here’s a Lua port:

local function Player_Move(player, walkDirection, relativeToCamera)
	if player.Character then
		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:Move(walkDirection, relativeToCamera)
		else
			warn("Player:Move called, but player currently has no humanoid.")
		end
	else
		warn("Player:Move called, but player currently has no character.")
	end
end

MoveTo on the other hand, tells the Humanoid to try and walk towards a specific position. The Humanoid will automatically calculate a movement direction to use while its in a MoveTo state, and it won’t stop until it

  • Reaches its goal.
  • Times out because it couldn’t reach the goal (8 seconds)
  • A call is made to the Humanoid’s Move/MoveTo function again.

7 Likes

I’ve returned back to this topic once again after working other things for a while now. Is there a way I can get the Vector3 WalkDirection directly? Because currently, what I’m seeing is that walk direction is only for you to input.

3 Likes

Try using “Humanoid.MoveDirection”. It gives the specific WalkDirection and you can just put that in.

Play your game (in studio), go to game.Workspace and find your player there. If you delete the Humanoid the output will be spammed with something like “Player:Move called, but player currently has no humanoid”. So the core scripts use Humanoid:Move

1 Like