Alternative to Mouse.TargetFilter?

I want my mouse to ignore parts that have CanCollide set to false. Sadly, Mouse.TargetFilter does not work with tables. I’ve seen there’s a solution to place all those ignore parts in a folder, but I can’t do that in my game. Is there any other way to set ignore list of mouse to a table?

3 Likes

You can do your own raycasts for the mouse!

Like this
local MAX_RAY_LENGTH = 5000
local mouse = game.Players.LocalPlayer:GetMouse()

function getMouseRay( rayLength )
	local rayLength = rayLength or 1
	return Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * rayLength)
end

function getMouseTarget(  )
	return game.Workspace:FindPartOnRay( getMouseRay(MAX_RAY_LENGTH) )
end

function getMouseTargetWithIgnoreList( ignorelist )
	return game.Workspace:FindPartOnRayWithIgnoreList( getMouseRay(MAX_RAY_LENGTH), ignorelist )
end

function getMouseTargetWithWhitelist( whitelist )
	return game.Workspace:FindPartOnRayWithWhiteList( getMouseRay(MAX_RAY_LENGTH), whitelist )
end

function getNonCanCollideParts(  )
	--[[Your choice of how to do this. You *could* build the list by iterating over every 
	instance in workspace each time, but that'd be pretty slow. My choice would be to use
	CollectionService and tag every non- CanCollide part when the game starts and when a 
	Part is added to Workspace. 

	E.g.:
	return TagS:GetTagged("NonCanCollide")
	]]
end

function getMouseTargetIgnoreNonCanCollide( )
	return getMouseTargetWithIgnoreList( getNonCanCollideParts() )
end
2 Likes

Thanks for the solution! I’ll try it out. Also, would this reduce game performance compared to mouse.Target?

mouse.Target also uses some kind of raycast to find the Part under the mouse, so they should be about the same speed. If you build the list of non-cancollide list from scratch every time you do a raycast, that would be a huge speed bottleneck. If you do raycasts against whitelists that don’t contain a lot of parts, that should speed thing up a bit.

1 Like

I have found a good work-around.

In the table you create, assuming you’re doing it locally, set any parts that are troubling your mouse.Hit accuracy to be CanQuery = false.

Mouse.Hit cannot see objects that have CanQuery set to false.

My issue that lead to this discovery, was that I was using Mouse.Hit.Position for Raycasting, but layered clothing and accessories would create a slight inaccuracy that got exaggerated at closer ranges. If you wore a giant piece of layered clothing that covered your character, you were basically impossible to shoot.

		local IgnoreList = {vPlayer.Character, camera}
		for _, item in pairs(workspace:GetChildren()) do
			if item:IsA("Model") and item:FindFirstChild("Humanoid") then
				for _, part in pairs(item:GetDescendants()) do
					if (part.Parent:IsA("Accessory") or part.Parent:IsA("Hat")) and part:IsA("BasePart") then
						table.insert(IgnoreList, part)
						part.CanQuery = false
						part.CanTouch = false
					end
				end
			end
		end
1 Like