Raycast always returning nil

Localscript →

local re = game.ReplicatedStorage.Events.RemoteEvent
local mouse = game.Players.LocalPlayer:GetMouse()
local Camera : Part = game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
mouse.Button1Up:Connect(function()
	re:FireServer(Camera.Position,mouse.Hit.Position) -- this seems to be the problem
end)

this seems to be the problem “mouse.Hit.Position”

re:FireServer(Camera.Position,mouse.Hit.Position) -- the problem

ServerScript →
IF NEEDED

remoteevent.OnServerEvent:Connect(function(Plr : Player, CameraPos:Vector3,MousePos : Vector3, BlockId : number)
		local RP = RaycastParams.new()
		RP.FilterDescendantsInstances = {Plr.Character}
		local RC = workspace:Raycast(CameraPos, MousePos, RP)
		if RC then
			local Touched : BasePart = RC.Instance
			local Part = Instance.new("Part")
			Part.Size = Vector3.new(2,2,2)
			local ProperPos = RC.Position+Vector3.new(0,(Part.Size.Y/2),0)
			print(Part.Size.Y/2)
			local PositionX = math.round(ProperPos.X / grid) * grid
			local PositionY = math.round(ProperPos.Y / grid) * grid
			local PositionZ = math.round(ProperPos.Z / grid) * grid
			Part.Position = Vector3.new(PositionX, PositionY, PositionZ)
			Part.Anchored = true
			Part.Parent = workspace
		end
	end)

Thanks for any help!

It may be returning nil because it is firing at the incorrect place or is never reaching its target.

Firstly, for the ray’s direction, you need to subtract MousePos from CameraPos; this will get a Vector3 representing the distance between the two objects with CameraPos as the reference.

Next, you can multiply this result to ensure the ray hits its intended target. For example, you can multiply it by 2 and it should work.

2 Likes

The second parameter of workspace:Raycast() is a direction vector. You are using the mouse position instead of the direction the ray needs to travel to from the camera. You can calculate the direction by doing CameraPos - MousePos instead.

1 Like

ill try that! But why do we need to do that? (it doesn’t really make sense to subtract these two things)

This gets the direction of the mouse compared to the camera. Raycast uses direction instead of plain locations. By subtracting your target position (MousePos) from your ray origin (CameraPos), it gives the direction of where the mouse is located compared to the camera.

1 Like

I have updated the part of the script to

local RC = workspace:Raycast(CameraPos,(CameraPos - MousePos) *10, RP)

even with “* 2” doesnt work
yet it doesnt work??

Can you mention what this script is being used for? That way I can get context.

In the meantime, try adding a FilterType to the RP.
I’m assuming Blacklist will work for your case (which ignores character)

It’s used for a grid placement system. Nvm i fixed it by literally just switching the “CameraPos - MousePos” around so its "MousePos- CameraPos ", Also who do i mark as the solution??? (You guys both had the same answer :disappointed_relieved:). I have a FilterDescendantsInstances

I think you hit the three dots on a post or something named “mark as solution.” It has a checkbox as the symbol.

And yeah, usually switching those two values around get the right result lol.

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