How can I get the direction a player is facing?

I want to make it that a part is positioned a few studs in front of a player, but in the direction the player is facing. Like if the player is facing left, then the part should be positioned a few studs to the left. The problem is I don’t know how to get the direction the player is facing.

4 Likes

You can position it in front of the player using the lookVector of the player:

part.Position = playerHRP.Position + playerHRP.CFrame.LookVector * STUDS_DISTANCE
21 Likes

Your gonna have to specify what you mean by playerHRP. It could be the player your talking about, the character model, or a part instance of the character model.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	wait(3)
	print("Creating part instance in front of player..")
	
	local part = Instance.new("Part")
	part.Parent = workspace 
	-- Yes, I know this is bad practice.

	local success, err = pcall(function()
		-- lookVector of player Character model
		local chr = player.Character
		part.Position = chr.Position + chr.CFrame.LookVector * 5
	end)
	
	print(err)
	-- lookVector of player instance
	part.Position = player.Position + player.CFrame.LookVector * 5
end)

--[[      [-- Output --]
  15:47:41.840  Creating part instance in front of player..  -  Server - PlayerDirection:9
  15:47:41.840  Position is not a valid member of Model "Workspace.Varsitelle"  -  Server - PlayerDirection:19
  15:47:41.840  Position is not a valid member of Player "Players.Varsitelle"  -  Server - PlayerDirection:20
  15:47:41.840  Stack Begin  -  Studio
  15:47:41.840  Script 'ServerScriptService.PlayerDirection', Line 20  -  Studio - PlayerDirection:20
  15:47:41.840  Stack End  -  Studio
]]

If it hadn’t been obvious at all, playerHRP refers to the player’s character’s HumanoidRootPart

8 Likes

Experienced developers on the Roblox Studio might be able to pick up on what you mean by HRP. But some players excluded from that category might not.

It just so happens that I’m excluded from that category.
What may be obvious to you may not be for some others.

12 Likes

I think poster Oob was asking about player or player’s client-sided current camera but not character or character’s HumanoidRootPart :sweat_smile: