Raycast always printing nil

I honestly dont know what im doing wrong. Im new to raycasts and when i tried to make one myself it always prints nil in the output even if the mouse is clicking something

Local Script:

local Tool = script.Parent

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

	local Handle = Tool.Ray.Position
	local MousePosition = Mouse.Hit.Position
	local Direction = (Handle - MousePosition).Unit * 2
	local RayResult = workspace:Raycast(Handle, Direction)

There are 2 reasons you are getting nil from your raycast.

1 - (Handle - MousePosition) should really be (MousePosition - Handle) since you want to get the direction from the handle to the mouse and not the direction from the mouse to the handle.

2 - Currently the length of Direction is 2 studs which is not very far, try increasing it to a bigger value like 20.

I hope this can help (:

1 Like

Thank you so much, I figured this out a little earlier.

1 Like

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