Raycast results returning nil

im working on a placement system and im having issues with raycasting. its returning nil.
here is the script, i think i must have done something wrong but i am not sure what

local rayparams2 = RaycastParams.new()
			rayparams2.FilterType = Enum.RaycastFilterType.Whitelist
			rayparams2.FilterDescendantsInstances = {hitbox}
			local ray1 = workspace:Raycast(hitbox.Position+Vector3.new(hitbox.Size.X,0,0),hitbox.Position,rayparams2)
			local ray2 = workspace:Raycast(hitbox.Position+Vector3.new(0,hitbox.Size.Y,0),hitbox.Position,rayparams2)
			local ray3 = workspace:Raycast(hitbox.Position+Vector3.new(0,0,hitbox.Size.Z),hitbox.Position,rayparams2)

Your filter type is set to whitelist, are you sure you want to filter out everything besides the hitbox?

i want the raycast to only detect hitbox

I think that there might be an issue with you adding size to the origin point, since the raycast might just spawn below or next to the hitbox.

what do you mean by “the raycast might just spawn below or next to the hitbox”?
if im setting the origin to position - size.X then it should spawn next to the hitbox, shouldnt it

No, my bad. I forgot a lot about raycasts, and that they also have a LookVector property :sweat_smile: Either way, I’ll read through your script once again.

Is your hitbox part a singular part?

here is a picture of hitbox
image

Alright, I have a conclusion. If only the hitbox part is the hitbox, then there is a logic error in your script, that being; you are filtering everything besides the descendants of the part, and not the part itself, therefore, the part is still blacklisted from being hit.

Therefore, there is a very simple solution. If you can put a descendant that has exactly the same size and is positioned the same then it should all work, try doing that.

image
not sure if thats what you mean but that doesnt work

Can you please send a photo with what you are trying to do? And create parts on the ray size/CFrame to see what ray is wrong and what is wrong.

sure, here is the full script. The raycasts are on lin 189, so near end

local module = {}
local RR3 = require(game.ReplicatedStorage.RotatedRegion3)

module.place = function(plr,item)
	local placeitem = game.ReplicatedStorage.Remotes.PlaceItem
	local canPlace = false
	local itemTransparent = item:Clone()
	itemTransparent.Parent = workspace.Placing
	local function refreshcolor()
		if canPlace == true then
			for i,v in pairs(itemTransparent.Parts:GetChildren()) do
				if v.Name == "Hitbox" then
					v.Transparency = 0.5
					v.Color = Color3.fromRGB(0, 214, 0)
					v.CanCollide = false
				else
					v.Transparency = 0.5
					v.CanCollide = false
				end
			end
		else
			for i,v in pairs(itemTransparent.Parts:GetChildren()) do
				if v.Name == "Hitbox" then
					v.Transparency = 0.5
					v.Color = Color3.fromRGB(200, 0, 0)
					v.CanCollide = false
				else
					v.Transparency = 0.5
					v.CanCollide = false
				end
			end
		end
	end
	refreshcolor()
	local blacklist = {}
	local plot
	for i,v in pairs(game.Workspace.Plots:GetChildren()) do
		if v.Owner.Value == plr.Name then
			plot = v
			for i,b in pairs(v.BaseParts:GetChildren()) do
				b.Grid.Transparency = 0
			end
		end
	end
	for i,v in pairs(itemTransparent.Parts:GetChildren()) do
		table.insert(blacklist,v)
	end
	for i,v in pairs(plot.BuildAreas:GetChildren()) do
		table.insert(blacklist,v)
	end
	local mouse = plr:GetMouse()
	local hitbox = itemTransparent.Parts.PrimaryPart
	local function snapToGrid(vector3,middle,folder,posx,posz,normal,hit,S1,S2,S3)
		local x = math.floor(vector3.X+0.5)
		local y = vector3.Y
		local z = math.floor(vector3.Z+0.5)
		wait()
		local nx,ny,nz = normal.X,normal.Y,normal.Z
		if nx ~= 0 and (ny == 0 and nz == 0) then
			if nx > 0 then
				print("Front")
				if S1 == "Right" or S1 == "Left" then
					x -= hitbox.Size.X/2
				elseif S1 == "Top" or S1 == "Bottom" then
					x -= hitbox.Size.Y/2
				elseif S1 == "Front" or S1 == "Back" then
					x -= hitbox.Size.Z/2
				end		
			else
				print("Back")
				if S1 == "Right" or S1 == "Left" then
					x += hitbox.Size.X/2
				elseif S1 == "Top" or S1 == "Bottom" then
					x += hitbox.Size.Y/2
				elseif S1 == "Front" or S1 == "Back" then
					x += hitbox.Size.Z/2
				end	
			end
		elseif ny ~= 0 and (nx == 0 and nz == 0) then
			if ny > 0 then
				print("Top")
				if S2 == "Right" or S2 == "Left" then
					y += hitbox.Size.X/2
				elseif S2 == "Top" or S2 == "Bottom" then
					y += hitbox.Size.Y/2
				elseif S2 == "Front" or S2 == "Back" then
					y += hitbox.Size.Z/2
				end	
			else
				print("Bottom")
				if S2 == "Right" or S2 == "Left" then
					y -= hitbox.Size.X/2
				elseif S2 == "Top" or S2 == "Bottom" then
					y -= hitbox.Size.Y/2
				elseif S2 == "Front" or S2 == "Back" then
					y -= hitbox.Size.Z/2
				end	
			end
		elseif nz ~= 0 and (nx == 0 and ny == 0) then
			if nz > 0 then
				print("Left")
				if S3 == "Right" or S3 == "Left" then
					z -= hitbox.Size.X/2
				elseif S3 == "Top" or S3 == "Bottom" then
					z -= hitbox.Size.Y/2
				elseif S3 == "Front" or S3 == "Back" then
					z -= hitbox.Size.Z/2
				end	
			else
				print("Right")
				if S3 == "Right" or S3 == "Left" then
					z += hitbox.Size.X/2
				elseif S3 == "Top" or S3 == "Bottom" then
					z += hitbox.Size.Y/2
				elseif S3 == "Front" or S3 == "Back" then
					z += hitbox.Size.Z/2
				end	
			end
		end
		x = math.floor(x+0.5)
		z = math.floor(z+0.5)
		if itemTransparent.Parts.PrimaryPart.Size.X % 2 ~= 0 then
			if posx > x then
				x += 0.5
			else
				x -= 0.5
			end
		end
		if itemTransparent.Parts.PrimaryPart.Size.Z % 2 ~= 0 then
			if posz > z then
				z += 0.5
			else
				z -= 0.5
			end
		end
		folder.Parts:SetPrimaryPartCFrame(middle+Vector3.new(-x,y,-z))
		local rogi = {
			{1, 1, -1},  --top front right
			{1, -1, -1}, --bottom front right
			{-1, -1, -1},--bottom front left
			{-1, 1, -1}, --top front left

			{1, 1, 1},  --top back right
			{1, -1, 1}, --bottom back right
			{-1, -1, 1},--bottom back left
			{-1, 1, 1}  --top back left
		}
		local buildareas = plot.BuildAreas:GetChildren()
		local boolTable = {}
		for i,v in pairs(rogi) do
			local CornerPos = hitbox.CFrame * CFrame.new(hitbox.Size.X/2 * v[1], hitbox.Size.Y/2 * v[2], hitbox.Size.Z/2 * v[3])
			local region = RR3.new(CornerPos,Vector3.new(0,0,0))
			local partsfound = region:FindPartsInRegion3WithWhiteList(buildareas)
			local allpartsfound = region:FindPartsInRegion3()
			if #partsfound == 0 then
				table.insert(boolTable,false)
			else
				table.insert(boolTable,true)
			end
			for i,v in pairs(allpartsfound) do
				if v.Name == "Hitbox" and v ~= hitbox then
					table.insert(boolTable,false)
				end
			end
		end
		if not table.find(boolTable,false) then
			canPlace = true
		else
			canPlace = false
		end
		refreshcolor()
	end
	local RS = game:GetService("RunService")
	local UIS = game:GetService("UserInputService")
	itemTransparent.Parent = workspace.Placing
	RS.RenderStepped:connect(function()
		local ray = Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction*1000)
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Blacklist
		params.FilterDescendantsInstances = blacklist
		local results = workspace:Raycast(ray.Origin,ray.Direction,params)
		if results ~= nil then
			local posX = plot.Middle.Position.X - results.Position.X
			local posY = results.Position.Y - plot.Middle.Position.Y
			local posZ = plot.Middle.Position.Z - results.Position.Z
			local rayparams2 = RaycastParams.new()
			rayparams2.FilterType = Enum.RaycastFilterType.Whitelist
			rayparams2.FilterDescendantsInstances = {hitbox}
			local ray1 = workspace:Raycast(hitbox.Position+Vector3.new(hitbox.Size.X,0,0),hitbox.Position,rayparams2)
			local ray2 = workspace:Raycast(hitbox.Position+Vector3.new(0,hitbox.Size.Y,0),hitbox.Position,rayparams2)
			local ray3 = workspace:Raycast(hitbox.Position+Vector3.new(0,0,hitbox.Size.Z),hitbox.Position,rayparams2)
			local function getsurface(surfacenormal)
				local normal = hitbox.CFrame:VectorToObjectSpace(surfacenormal)
				local nx,ny,nz = normal.X,normal.Y,normal.Z
				if nx ~= 0 and (ny == 0 and nz == 0) then
					if nx > 0 then
						print("Front")
						return("Front")
					else
						print("Back")
						return("Back")
					end
				elseif ny ~= 0 and (nx == 0 and nz == 0) then
					if ny > 0 then
						print("Top")
						return("Top")
					else
						print("Bottom")
						return("Bottom")
					end
				elseif nz ~= 0 and (nx == 0 and ny == 0) then
					if nz > 0 then
						print("Left")
						return("Left")
					else
						print("Right")
						return("Right")
					end
				end
			end
			local pos = Vector3.new(posX,posY,posZ)
			snapToGrid(pos,plot.Middle.CFrame,itemTransparent,results.Position.X,results.Position.Z,results.Normal,results.Instance,getsurface(ray1.Normal),getsurface(ray2.Normal),getsurface(ray3.Normal))
		end
		wait()
	end)
	UIS.InputBegan:Connect(function(input)
		print("input changed")
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print("mouse button pressed")
			if canPlace == true then
				if plr.PlaceCooldown.Value == false then
					if plr.Inventory.Structures:FindFirstChild(item.Name) ~= nil then
						placeitem:FireServer(item.Name,itemTransparent.Parts.PrimaryPart.CFrame)
					end
				end
			end
		elseif input.KeyCode == Enum.KeyCode.R then
			plot.Middle.Orientation += Vector3.new(0,90,0)
		elseif input.KeyCode == Enum.KeyCode.T then
			plot.Middle.Orientation += Vector3.new(90,0,0)
		end
	end)
end

return module

I don’t mean the script, I mean an Photo with how you want the Placement sistem to look like, and make an short script where you create an part along the Ray’s Size and CFrame (Like the old guns do)

the placement system is almost done. what i am trying to do is detect the surface of the hitbox, to find out how is it oriented so the script knows how to snap it to the surface
image

also thats a pic from like 1 day ago, because now its not working at all because of the raycasts

Can you do this please? By that I mean place an Anchored part in the middle of the Ray, and make its size the Ray’s size. Don’t forget about CFrame.LookAt to show the exact CFrame!

the hitbox is not even appearing so it wont work most likely but ill try

how do i do that tho, i have tried but it doesnt work
image

Also, Make the HitBox transparency 0 to make sure it is in the right place. And, I will try to give you an script to do the Part trick, but I’m on mobile so it might not work.

local Part = Instance.new("Part")
Part.CFrame = CFrame.lookAt(Ray.Origin-RayEnd,RayEnd)
Part.Size = Vector3.new(0.1,0.1,(Ray.Origin-RayEnd).Magnitude)

PartEnd is the Position of the RayResult

it is in the right place. ill try your script