Pass Table instead of Instance at Mouse.TargetFilter

Hello! I’m working at a gun, and I need it to be able to fire though invisible/cancollide off parts.

I cannot use Collisions groups

So I have 2 things I can do

Use Mouse.TargetFilter (I can’t pass a table, only Instances)

When the ray hits a part, if its invisible ray again from the place it got hit and ray again to the direction (I don’t know how)

Current code:

Client:

local DisableList = {}

local function Raycast(Pos)
	local hitRay = Ray.new(Tool.Handle.Position.Position,(Pos-Tool.FirePart.Position).Unit*Settings.Range.Value)
	local Hit,EndPos,Dir = workspace:FindPartOnRayWithIgnoreList(hitRay,DisableList,true,true)
	return Hit,EndPos,Dir
end

local function GetRaycast()
	local Hit,EndPos,Dir
	repeat
		Mouse.TargetFilter = {}
		Hit,EndPos,Dir = Raycast(Mouse.Hit.Position)
		if not Hit then
			break
		else
			if Hit:IsA("BasePart") then
				if not Hit.CanCollide or Hit.Transparency == 1 then
					table.insert(DisableList,Hit)
				end
			end
		end
	until true
	table.clear(DisableList)
	Mouse.TargetFilter = nil
	return Hit,EndPos,Dir
end

Errors that I can only use Instances in Mouse.TargetFilter

Server code:

local DisableList = {}

local function Raycast(Pos1,Pos2)
	local hitRay = Ray.new(Handle.Position,(Pos2-Pos1).Unit*Settings.Range.Value)
	local Hit,EndPos,Dir = workspace:FindPartOnRayWithIgnoreList(hitRay,DisableList,true,true)
	return Hit,EndPos,Dir
end

local function GetRaycast(Pos2)
	local Hit,EndPos,Dir
	local Pos1 = Tool.FirePart.Position
	repeat
		Hit,EndPos,Dir = Raycast(Pos1,Pos2)
		if not Hit then
			break
		else
			if Hit:IsA("BasePart") then
				if not Hit.CanCollide or Hit.Transparency == 1 then
					table.insert(DisableList,Hit)
					--Pos1  = EndPos
					--Pos2 = EndPos * ()
				end
			end
		end
	until true
	table.clear(DisableList)
	return Hit,EndPos,Dir
end

You can put all the parts you want the mouse to ignore in a folder and just set the TargetFilter to the folder, it’ll ignore the descendants of that folder as well

Can’t do that, as some parts moving into another Folder will break the game, and its obvious that I can’t just clone it and put in the folder

1 Like

The first one doesn’t work with Invisible parts
and the second one too

  1. Doesn’t fit my use case
  2. I said I can’t use CollisionGroups

wdym the first one fits your criteria:

Edit: Just gotta keep searching on google and the dev forumn until you find what you are looking for.

1 Like

Actually, just to clarify, RbxMouse does support invisible parts. That’s one of the reasons I made it. On the linked page you can see in the examples the following code:

-- Ignore parts with Transparency >= 1 or CanCollide == false
RbxMouse.RaycastMethod = RbxMouse.RAYCAST_FILTER_TRANSPARENCY_COLLIDABLE

There is also RAYCAST_FILTER_TRANSPARENCY, RAYCAST_FILTER_COLLIDABLE, and RAYCAST_FILTER_CUSTOM.

1 Like

I tried using it, however :UpdateTarget only returns a instance. Isn’t it supposed to return a table?

It returns a RaycastResult instance, which contains all of the information you could possibly get from a raycast using Roblox’s API. (Or nil if nothing was hit.)

Apparently, when trying to print it, it just returns the instance name. And when trying to say . Instance or . Position it says that its an instance and not RaycastResult

Ah, it looks you uncovered a bug, and also an inconsistency with my API.

I’ve fixed that in a new update – going forward, the result with always be a RaycastResult and not an Instance.

2 Likes