How to detect parts inside another part when both parts have CanCollide and CanQuerry set to false?

This is my current code (it’s inside a localscript) for a placement system. I want to know if the part i’m placing “ghost” is inside the players plot (which is an invis part), a reference to the plot is stored in the corresponding player as an ObjectValue. My current code does not work, i have also tried workspace:GetPartsInPart and both don’t work. I don’t see any problems though and i don’t get any errors but the part (ghost) remains red even when i move it inside the plot, meaning table.find returns nil, meaning it doesn’t detect the ghost part as being inside the plot. Does anyone know how to fix this?

     local item = localItem:WaitForChild("Part").Value
	local part = localItem:WaitForChild("Part").Value:WaitForChild("model").Value
	local ghost = part:Clone()
	ghost.Anchored = true
	ghost.CanCollide = false
	ghost.CanQuery = false	
	ghost.Transparency = .5
	ghost.Color = Color3.new(0, 1, 0)
	ghost.CFrame = CFrame.new()
	ghost.Parent = game.Workspace
	
	bind = RunService.RenderStepped:Connect(function()
		ghost.Position = mouse.Hit.Position
		
		local plot = localPlayer:WaitForChild("Plot").Value

		local region = Region3.new(plot.Position - (plot.Size / 2), plot.Position + (plot.Size / 2))
		local info = OverlapParams.new()


		local hitbox = game.Workspace:GetPartBoundsInBox(region.CFrame,region.Size,info)
		
		if table.find(hitbox,ghost) then
			ghost.Color = Color3.new(0,1,0)
		else
			ghost.Color	= Color3.new(1,0,0)
		end

	end)

Why does CanQuery need to be false?

2 Likes

Because i’m setting ghost.Position to mouse.Hit.Position which user canquery and if i set CanQuery to true on ghost mouse.Hit hits ghost itself so it kinda moves towards you and if i put CanQuery on the plot to true the part moves to the border of the plot but it can’t move inside the plot itself. So both of them have CanQuery turned off as to not mess with mouse.Hit

Just set Mouse.TargetFilter to ghost, or use raycasting manually for more control, unless you want to deal with all the complicated math.

2 Likes

I set mouse.TargetFilter to ghost and CanQuerry on ghost to true and that works but i can’t add multiple instances to mouse.TargetFilter so the problem with ghost getting stuck on the plot bounding box still remains.

It’s pretty simple to do the raycasting yourself and add infinite things to ignore

local RayInfo = RaycastParams.new()
RayInfo.FilterType = Enum.RaycastFilterType.Blacklist
RayInfo.FilterDescendantsInstances = {ghost, PlayerCharacter}

local function GetMousePosition()
    local MousePos2D = UserInputService:GetMouseLocation()
    local UnitRay = workspace.CurrentCamera:ViewportPointToRay(MousePos2D.X, MousePos2D.Y)
    
    local NewDir = UnitRay.Direction * 1000
    
    local Hit = workspace:Raycast(UnitRay.Origin, NewDir, RayInfo)
    
    return if Hit then Hit.Position else UnitRay.Origin + NewDir
end
1 Like