How would I access the direction of which my player's camera is facing?

Hey everyone,

I’m trying to make a script that moves a part in the direction that a player is facing, the issue is, the player’s lookvector never updates, and it always stays the same. I have no idea how to get through this, and I’ve tried a bunch of different things like using the Humanoid part / character part of my player. Any breakthroughs would be much, much appreciated.

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = localPlayer.Character:WaitForChild("Humanoid")
local lookvector = canera.LookVector
local runtime = game:GetService("RunService")
runtime.Stepped:Connect(function(deltaTime)
	print(lookvector)
end
	
)



local input = game:GetService("UserInputService")
local ebutton = Enum.KeyCode.E

input.InputBegan:Connect(function(input, gameProcessed)
		if input.KeyCode == ebutton then
	local remotedetonator = game.ReplicatedStorage.RemoteEvent
	remotedetonator:FireServer(lookvector)

		end
end)


local lookvector = canera.CFrame.LookVector

Should fix it, your also accessing the look vector once in this script, you have to put it inside a loop:

runtime.Stepped:Connect(function(deltaTime)
local lookvector = canera.CFrame.LookVector
	print(lookvector)
end

Got it, but it still doesn’t work. Here’s my local (script 1) and global (script2) scripts. I might have to use .stepped in the server too but I’m not sure.

Localscript
local Players = game:GetService("Players")
local runtime = game:GetService("RunService")

runtime.Stepped:Connect(function(deltaTime)	local localPlayer = Players.LocalPlayer
	
	local camera = workspace.CurrentCamera
	local character = localPlayer.Character:WaitForChild("Humanoid")
	local lookvector = camera.CFrame.LookVector
	
	local input = game:GetService("UserInputService")
	local ebutton = Enum.KeyCode.E

	input.InputBegan:Connect(function(input, gameProcessed)
		if input.KeyCode == ebutton then
			local remotedetonator = game.ReplicatedStorage.RemoteEvent
			remotedetonator:FireServer(lookvector)

		end
	end)
end)```
Normal Script
```lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local remotedetonator = game.ReplicatedStorage.RemoteEvent
remotedetonator.OnServerEvent:Connect(function(lookvector)
	
		local Part = workspace.Part
		local linearvelocity = Instance.new("LinearVelocity")
		local At0 = Instance.new("Attachment")

		Part.Anchored = false

		At0.Parent = Part
		linearvelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
		linearvelocity.LineVelocity = 10
		linearvelocity.LineDirection = Vector3.new(lookvector)
		linearvelocity.Parent = Part
		linearvelocity.MaxForce = math.huge 
		linearvelocity.RelativeTo =  Enum.ActuatorRelativeTo.Attachment0
		linearvelocity.Enabled = true
		linearvelocity.Attachment0 = At0
		

	end)```

The first argument the event takes is the player, its accessing the player instead of the look vector.

Replace it with something like this:

remotedetonator.OnServerEvent:Connect(function(player, lookvector)

wait I don’t understand, could you explain it in a little more detail?

the first parameter of the event when sending information from the client to the server is always going to be the player, so when you put lookvector as the first parameter, its actually getting the player instead of the look vector.

Replace the function with the code I put and it should work.

yeah I did, still doesn’t work.

I’m starting to think that I’m gonna need to have to use the .stepped in the serverside too, because the lookvector won’t update at all.

I think your forgetting about Attachment1 as you make an Attachment0 but not an Attachment1, im to lazy to look into linear velocity documentation, but I think you can find something about it.

Read the update, I changed it, and also, do I really need an attach1?

Im not sure, but I think your not applying any force, maybe VectorForce can change something???

I am because the part indeed moves, but not in the right direction.

OK I found something. I was debugging and I discovered that like you said, it only prints out the name of the player (I added a print(lookvector) to debug). I can’t get it to print the actual player’s lookvector though…

Ive got it!!!

TYSM dude

Have a good one

1 Like