raycast.Position is nil

Previous to my old issues with this, this seems like the only thing that is blocking the tool from actually functioning.

The main objective is to create a working gun and I’ve completed the majority of it, but this line of code errors whenever it is cast in specific spots like the sky and returns nil.

I, unfortunately, can’t provide a screenshot of the error as it is silently rendered. and won’t show up.

local dist = (tool.Muzzle.CFrame.Position - raycast.Position).magnitude

Everything else works fine, so I won’t include much more of the script unless asked. In which, click the arrow for the expanded code that seems to relate to this.

Extended code
local ta = game.Players.LocalPlayer:GetMouse()
repeat wait()
		oo = char:FindFirstChild("Head").Position
	until oo == char:FindFirstChild("Head").Position
local rc = RaycastParams.new() --Assume everything is working, cause this is just filter

local raycast = workspace:Raycast(oo, (ta - oo).Unit * 300,rc)
1 Like

WorldRoot:Raycast

Casts a ray using an origin, direction, and optional RaycastParams . If it finds an eligible BasePart or Terrain cell, a RaycastResult is returned containing the results of the operation.

Returns

RaycastResult: Contains the results of a raycast operation, or nil if no eligible BasePart or Terrain cell was hit.

This is all in the documentation. The sky is not a BasePart or a Terrain cell, therefore a RaycastResult isn’t returned. The sky is not an instance and so it cannot have any 3D positions either.

Raycast a bit differently from its deprecated counterpart(s), FindPartOnRay. You can obtain a position with this raycast method even if you cast into the sky because theoretically, raycasts have infinite length. Roblox raycasts are capped to a length of 5000 studs and so the position you get is the end of that cast in the sky 5000 studs out. Additionally, it returns its results as a tuple instead of an instance, so some of the returns can be nil while Raycast doesn’t return anything if the cast didn’t hit a valid object.

Here’s a thread discussing this problem that you can reference:

1 Like

Not sure if this is even fixable, but when I tried getting the direction, the raycast seems to go off the rails and not follow my mouse position. Any idea why this happens?

If the ray didn’t hit anything, of course it will be nil. You can manually calculate raycast.Position by just adding the direction to the origin.

local rayCastPosition = origin + direction

The reason you want to add direction to origin is because direction will be a vector positioned in a way closer to the origin, you want the direction to start from the origin (note that the direction will still be relative to the world origin except it will be positioned in a way if it was starting from origin).

Before:


The box is the origin and the black line is the direction.
After:
The purple line is origin + direction.

1 Like

I did, but now it acts weird. It just goes elsewhere, sometimes the opposite position where the mouse is hovering over.