Server Raycasting failing

Hello! I have ever since the last RobloxStudio I have been experiencing weird issue with workspace:Raycast(), it constantly returns nil. Here is my code:

BTW: none of the following links have worked: this, this, this and this

-- NOTE: This is being run server-side as a RemoteEvent.OnServerEvent
local NewRay = RaycastParams.new()

local ray = game.Workspace.CurrentCamera:ScreenPointToRay(ScreenCentre.X, ScreenCentre.Y)
print(ray)

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Player.Character}

local Cast = workspace:Raycast(ray.Origin, ray.Direction * 5000, Params)
local TargetLocation = Vector3.new(math.huge, math.huge, math.huge)
	
print(Cast)
	
if Cast then
	TargetLocation = Cast.Position
else
	ShowMessage(Player, "(!) Out of range")
end
	
print(TargetLocation)

The output being:

14:39:15.692  {-257.833771, -178.259277, 436.05072}, {-0.401448041, -0.344798326, 0.848500729}  -  Server - Script:5
14:39:15.692  nil  -  Server - Script:13
14:39:15.692  inf, inf, inf  -  Server - Script:21

Obviously the Camera:ScreenPointToRay() but the Raycasting doesn’t?
Any Ideas why?

Double check that the default filter type is “Exclude”, otherwise it might be excluding everything but the character.

How are you computing ScreenCentre? Have you verified that the screen center ray is correct?

I can’t see anything obviously wrong.

How far away is the thing you’re expecting the ray to hit? If it’s like 0.0001 studs or w/e then that might cause issues, not sure how ScreenPointToRay works but if it places the origin at the near clipping plane then the ray might miss things that are super close to the camera.

Anyway, if you just need a ray from the center of the screen you can use camera.Position and camera.CFrame.LookVector for the first 2 raycast parameters. It should work with your approach though.

Are you trying to hit something that doesn’t take up the entire screen? Mixing up ScreenPointToRay and ViewPointToRay might cause it to miss when you expect it to hit.

ScreenCentre.X and ScreenCentre.Y are both accurate as they are using Mouse.ViewSize, NewRay.FilterType is set to exclude.

TYSM, I’ve been trying to fix this for hours.

Edit: For the people viewing this after this was the fix:
local Cast = workspace:Raycast(Camera.CFrame.Position, Camera.CFrame.LookVector * 5000, Params)

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