How could I get the camera's look vector on the server side?

I’m trying to create a raycast from the player’s character to the camera’s LookVector on the server side. I’m currently sending the LookVector over a remote event.

Client side:

local LookVector = workspace.CurrentCamera.CFrame.LookVector
local function onButton1Down()
	Remote:FireServer(LookVector)
end
mouse.Button1Down:Connect(onButton1Down)

Server side:

Remote.OnServerEvent:Connect(function(LookVector)
	local length = 1000
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {player}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.IgnoreWater = true
	local raycastResult = workspace:Raycast(Root, LookVector * length, Params)
end)

Root is the humanoid root part.

I’m getting this error:
attempt to perform arithmetic (mul) on Instance and number

Any help appreciated!

1 Like

Maybe it’s because you are passing an instance in the Remote event which the server cannot access? Not sure but try doing this:

local function onButton1Down()
    local value = LookVector
	Remote:FireServer(value)
end

I can’t use the word “LookVector” as my variable name?

1 Like

Yeah? because you already defined it outside the function…

1 Like

The first parameter should be player

2 Likes

I still get the same error when defining it within the function:


local function onButton1Down()
	local LookVector = workspace.CurrentCamera.CFrame.LookVector
	Remote:FireServer(LookVector)
	print("sent input to the server")
end

Like MrOnlyKemal said, you have to let the first argument in the server side script be the player.

Remote.OnServerEvent:Connect(function(player, LookVector)

Ok but now I get a new error:

Unable to cast Instance to Vector3

@MrOnlyKemal

Make sure you adjusted your function to include the player as the first value!
Right now your LookVector variable is being set to the player, which is why it gives the Unable to cast Instance to Vector3 error.

Remote.OnServerEvent:Connect(function(player, LookVector)
	local length = 1000
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {player}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.IgnoreWater = true
	local raycastResult = workspace
end)

I am already passing player as the first value and still getting this error, as I mentioned in my last message.

Anyways the player is sent over automatically without having to define it in the remote. When I define both the player and the look vector on the server side, I receive my original error:

Client side:

local function onButton1Down()
	local LookVector = workspace.CurrentCamera.CFrame.LookVector
	Remote:FireServer(player, LookVector)
	print("sent input to the server")
end

Server side:

Remote.OnServerEvent:Connect(function(player, LookVector)
	local length = 1000
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {player}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.IgnoreWater = true
	local raycastResult = workspace:Raycast(Root, LookVector * length, Params) -- this line is causing the error according to the output
end)

Error:
attempt to perform arithmetic (mul) on Instance and number

It’s because length is just a number and not a cframe.

1 Like

This is the cause! You need to do Root.Position instead of just Root for the first argument!

local raycastResult = workspace:Raycast(Root.Position, LookVector * length, Params)

1 Like

This is correct too. (I am not exactly sure, but pay attention to this)

When I take length out and do Root.Position it gives me this error:
Screenshot 2024-04-12 162838

@JohnsonBlu @robot300047

FireServer doesnt take a player parameter.

local function onButton1Down()
	local LookVector = workspace.CurrentCamera.CFrame.LookVector
	Remote:FireServer(LookVector)
	print("sent input to the server")
end
Remote.OnServerEvent:Connect(function(player, LookVector)
	local length = 1000
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {player.Character}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.IgnoreWater = true
	local raycastResult = workspace:Raycast(Root, LookVector * length, Params) -- this line is causing the error according to the output
end)
2 Likes

The only issue seems to be that you are not assigning the “Player” variable in the OnServerEvent function. You’re code logic seems quite fine other than that.

hmmmmm im really at my wits end here idk
if i think of something ill edit it in here

Ima start testing in roblox studio and see if I can find something, brb.

EDIT: Allright, this code works for me:

Client:

local function onButton1Down()
    local LookVector = workspace.CurrentCamera.CFrame.LookVector
	Remote:FireServer(LookVector)
end

mouse.Button1Down:Connect(onButton1Down)

Server:

Remote.OnServerEvent:Connect(function(player, LookVector)
	local length = 1000
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {player.Character}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.IgnoreWater = true
	local raycastResult = workspace:Raycast(Root.Position, LookVector * length, Params)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.