RaycastResult is returning nil

  1. What do you want to achieve? I want to cast a ray from the mouse and get information about the ray.

  2. What is the issue? Players.CheeseGodTheEpic.PlayerGui.MainClient:70: attempt to index nil with 'Instance'

  3. What solutions have you tried so far? I’ve looked on the Developer Forum, none of the answers seemed to be my solution.

RunService.RenderStepped:Connect(function()
			local mouseRay = mouse.UnitRay
			local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
			
			local raycastParams = RaycastParams.new()
			--raycastParams.FilterDescendantsInstances = {character}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			
			local raycastResult = game.Workspace:Raycast(mouseRay.Origin, mouseRay.Direction)
			--and raycastResult.Instance.Name == "Placeable"
			print(raycastResult.Instance)
end)

Thanks in advance!

Do if raycastResult then beforehand.

That way you don’t attempt to index a nil value with the key/field “Instance”.

workspace:Raycast() can return nil.

1 Like

You forgot the distance factor(1000), also you where not using raycastParams in your raycast:

local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction*1000, raycastParams)

The two answers above are correct but its probably worth adding that Ray.new() is deprecated and LocalPlayer:GetMouse() has been superseded by UserInputService

1 Like

Ray.new() IS NOT DEPRECTATED. The old methods of reading a raycast like findpartsonray() ARE DEPRECATED. You can still use Ray.new() for the new type of raycast(as long as it’s with the Ray.Origin etc)

2 Likes

Thanks for the correction, although there’s still not much use for Ray.new() I can find besides Ray:ClosestPoint() (which can still be achieved with WorldRoot:Raycast() btw), just generally would not recommend using it for typical raycasting purposes.

Yeah. Ray.new is useless. But sometimes it can be useful(rarely)

1 Like