Move player forward according to the direction they're facing

I am relatively new to Roblox scripting, and I want to move the player forward according to the direction they’re facing when they click on a part. The only problem is that I don’t know how to script that. I cannot find any solutions to what I am asking online. Help would be appreciated!

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local hrp = player.Character:WaitForChild('HumanoidRootPart')
	hrp.Position = hrp.Position + Vector3.new(5,0,0)
end)

This script only moves the player along the X-axis.

2 Likes

The player character is a model, where if you try to change the position of any one part, it will not effect the rest of the model. To move an entire model, you must change the CFrame of it’s PrimaryPart (which in a player is the HumanoidRootPart).

The “forward” direction in roblox is the negative Z direction, so you can move the player with:

hrp.CFrame *= CFrame.new(0, 0, -5)
1 Like

theres two ways you can do it.

  1. the way their character model is facing
hrp.CFrame = hrp.CFrame.LookVector * 5

all baseparts in roblox have a CFrame and all CFrames have three direction vectors. LookVector is the one that points in the forward direction (the direction the block is facing). There are also RightVector and UpVector. And if you want to go in the opposite direction of any of those you can just multiply them by a negative number.

  1. you can use the player camera’s direction (only on local scripts)
local plyrCam = game:GetService("Workspace").CurrentCamera


hrp.Position = plyrCam.CFrame.LookVector * 5

------------EDIT-------------
No biggy but i just remembered that you dont need to move the HRP to move a character model. You can use a function that all models have.

local char = player.Character
local ppCF = char:GetPrimaryPartCFrame()

char:SetPrimaryPartCFrame( ppCF.LookVector * 5 )
1 Like

SetPrimaryPartCFrame() is only valid if the model has a primary part assigned.

Character:PivotTo(Character:GetPivot() * CFrame.new(0, 0, -5))
8 Likes

All player character models have a primary part.