Issue with TargetFilter and having it detect a part with specific name

I want it so that if a player’s mouse is over an item named Hitbox it will be ignored and instead will only read the parts behind it.

The problem I’m having is that when i hover my mouse over the part that I want selected, it glitches out and cant seem to update the TargetFilter part in time.

The most common solution I’ve found on the forums was to use raycasting, but I’m not too knowledgeable with it, and it could potentially be taxing on performance.

Here’s an example of what I have using TargetFilter:

local UIS = game:GetService("UserInputService")

UIS.InputChanged:Connect(function(input)
	if mouse.Target then
		if mouse.Target.Name == ("Hitbox") then
			mouse.TargetFilter = mouse.Target
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if mouse.TargetFilter then
		--do stuff here
	end
end)

Any ideas on how I could more effectively use the TargetFilter for ignoring a part with a specific name?

No, the way you’re doing it is fine. You could change it to something like this:

local UIS = game:GetService("UserInputService")

UIS.InputChanged:Connect(function(input)
	local target = mouse.Target
	if target then
		if target.Name == ("Hitbox") then
			mouse.TargetFilter = mouse.Target
			target = mouse.Target --update the target variable to a target that is not the filtered one
		end

		--do something to the target
	end
end)

But it really depends on what you’re trying to do.

But a different approach is to use raycasting, like you suggested. It’s not that hard, and no it’s not going to be a performance problem. I would also use CollectionService to tag every Hitbox instead of relying on the Name property, because that’s exactly what it’s for. Plus, it can correctly handle the case where there’s some valid target between two hitboxes, which your solution with mouse.TargetFilter doesn’t.

local UIS = game:GetService("UserInputService")
local TagS = game:GetService("CollectionService")

function getMouseRay()
	return Ray.new(
		mouse.UnitRay.Origin, 
		mouse.UnitRay.Direction * 5000 --Direction is a unit vector, 5000 studs is the max length
	)
end

function findValidTarget()
	local hitboxes = TagG:GetTagged("Hitbox")
	return game.Workspace:FindPartOnRayWithIgnoreList(
		getMouseRay(),
		hitBoxes
	)
end

UIS.InputChanged:Connect(function(input)
	local target = findValidTarget()
	if target then
		
		--do something to the target
	end
end)

EDIT: If you go the CollectionService route, you’ll need a tag editor plugin. Just search in the plugin marketplace for a good one.

3 Likes

Interesting! The tag editor is the most helpful thing I’ve ever seen