Raycast is returning nil

I send the mouse position from the client to the server with a remote event and then create a ray on the server. The mouse position does get sent to the server, the ViewRay variable also gets created, however the ray returns nil for some reason. Why? I am standing right next to the part I want to hit, so I can’t be too far away.

Client script:

UserInputService.InputBegan:Connect(function(Input)
					if Input.UserInputType == Enum.UserInputType.MouseButton1 then			
                       SomeRemote:FireServer(UserInputService:GetMouseLocation())
					end
				end)

Server script:

SomeRemote.OnServerEvent:Connect(function(Player, MousePosition)

	local ViewRay = workspace.Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y, 0)

	local ray = workspace:Raycast(ViewRay.Origin, ViewRay.Direction * 300)
	print(ray)--Prints nil
end)
2 Likes

You’re using the workspace.Camera on the server, not the client, you should get the ViewRay on the client script and send it as an argument to the server with your RemoteEvent.

3 Likes

This worked. Does this have to do with getting the specific Player’s camera on the client (something that the server can’t do) or that ViewportPointToRay works only on the client?

2 Likes

Camera properties only changing on each client, best option will be to set ray origin and direction as FireServer arguments

2 Likes

Your camera is only moved on the client, so the server has no idea about such modifications.

3 Likes

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