Setting TargetFilter ignores and then does not ignore alternatively?

localscript:

local tool = script.Parent.Parent
local isenabled = false
local plr = game.Players.LocalPlayer
local char = plr.Character
local debounce = false
tool.Activated:Connect(function()
	isenabled = true
	
	
	while true do
		wait()
		if isenabled == true and not debounce then
			debounce = true
			local mouse = plr:GetMouse()
			local mousepos = mouse.Hit
			local mousetarget = mouse.Target
			mouse.TargetFilter = mousetarget
			print(mouse.TargetFilter)
			
			script.Parent.SendMouse:FireServer(mousepos,mousetarget)
			wait(0.5)
			debounce = false
		end
	end
	
end)



tool.Deactivated:Connect(function()
	isenabled = false
end)

regular script:

script.Parent.SendMouse.OnServerEvent:Connect(function(plr,mousepos,mousetarget)
	
	
	
	local copy = game.ServerStorage.Projectiles.MelterShot:Clone()
	copy.Parent = game.Workspace
	copy:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.FirePoint.Position,mousepos.Position))
copy.CoreFolder.Go.Disabled = false
copy.CoreFolder.TeamID.Value = script.Parent.Parent.Parent.InfoFolder.TeamID.Value
end)

everything works fine except how TargetFilter behaves, when fired, the targetfilter DOES filter
but when another shot is fired, the targetfilter does NOT filter, it alternates back and forth and im not sure why its doing this.

targetfilter equals what Mouse.Target is. there are cancollide false parts in the workspace the mouse needs to ignore.

TargetFilter only works with one instance. If you want to have it work with multiple parts then you should use other functions to get mouse position. (Mouse is being phased out)
ViewportPointToRay should work for getting the target location of the mouse (GetMouseLocation is good for getting the mouse location)

1 Like

Mouse is not being phased out, where did you get this information? There is a way to have it work with multiple parts too, just set the TargetFilter to a group/folder that just the ignored parts are descendants of.

1 Like

do you mean making copies of the objects so the targetfilter knows what to ignore or putting the acual objects all into a folder? because I cant just put everything in a folder as they need to be in the workspace or another specific place.

I don’t mean removing it, but I presume in the future that it will be deprecated at some point.
In the documentation for mouse it already mentions that it is being superseded by UserInputService.

It says by and large, and it seems like it wants people to use UserInputService when there’s overlap since it mentions that in the same section. The Target and Hit functionalities are some the few things that Mouse has over UserInputService.

Mouse.TargetFilter doesn’t “stack” up.
When you change the TargetFilter it will overwrite the previous one so then the old one wouldn’t be ignored anymore.

Like I said in your other post, put all the parts into one single folder and then assign the target filter to that folder.

Or, if you don’t want to do it manually, write code to parent all the CanCollide = false parts in a folder.

Example:

local Ignore = Instance.new("Folder", workspace)
Ignore.Name = "Ignore"
for _,Obj in pairs(workspace.Map:GetChildren()) do
	if Obj:IsA("Part") and not Obj.CanCollide then
		Obj.Parent = Ignore
	end
end

(This is to be placed in the Local Script so that it doesn’t make changes to the server. This code is also untested).

You’d have to put the parts in a folder to use TargetFilter like that, you can use ObjectValues in their old locations to point to them or use @Halalaluyafail3’s method with ViewportPointToRay

just as an example, I have turrets inside another model, and inside the turret is a part called “Range”, range cannot be anywhere else and needs to be inside the turret, and range is the part we need to ignore. so im not sure if you mean make copies so it knows what to ignore or put the original inside the folder.

If you change the parent of the part locally on the client, it wouldn’t actually change the parent of the turret part on the server.

Read the text within the brackets.

this is a lot more work than it needs to be. im able to set the ignorelist. so it must be capable of running properly right? isnt there something I can do to have the ignored part stay ignored instead of turning off or on?

I’ve created a simple function which gets what the mouse is targeting and where it hits a part. (nil if it hits no part)
First return value will be where it hits (if it doesn’t hit then it will extend 1000 studs out)
Second return value will be what the mouse hits (max 1000 studs out, and if it doesn’t hit a part then it will be nil)

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Filter = {LocalPlayer.Character}
local function GetMouseHit()
	local location = UserInputService:GetMouseLocation()
	local unitray = Camera:ViewportPointToRay(location.X,location.Y)
	local ray = Ray.new(unitray.Origin,unitray.Direction*1000)
	local hit,intersect = workspace:FindPartOnRayWithIgnoreList(ray,Filter)
	local mousehit = CFrame.new(intersect)*CFrame.fromOrientation(CFrame.new(unitray.Origin,intersect):ToOrientation())
	return mousehit,hit
end

It also accounts for orientation just like mouse.Hit

The issue with Mouse.TargetFilter is that you can’t assign lists.

Mouse.TargetFilter = {workspace.Part, workspace.Range}

This would not work.

Therefore, by placing all the ignore parts into a folder, it will act similar to a list.

Unfortunately, I think this is the only method to “stack” parts for Mouse.TargetFilter.

1 Like

it doesnt have to really be a table or list, just the part in front of it(such as range)
would I be able to reset the targetfilter each time it fires and then reassign the part in front of it to the targetfilter?

Mouse is the base class for member mouse objects, it’s not going to be deprecated. It’s also remaining supported for legacy purposes and due to other API requiring it.

Inherited classes:
https://developer.roblox.com/api-reference/class/PlayerMouse
https://developer.roblox.com/api-reference/class/PluginMouse

2 Likes

You could do that however, this method has disadvantages.

Anyways, here’s a modified version of your code.

tool.Activated:Connect(function()
	isenabled = true
	while true do
		wait()
		if isenabled == true and not debounce then
			debounce = true
			local mouse = plr:GetMouse()
			mouse.TargetFilter = nil
			local mousetarget = mouse.Target
			if not mousetarget.CanCollide then
				mouse.TargetFilter = mousetarget
			end
			local mousepos = mouse.Hit
			script.Parent.SendMouse:FireServer(mousepos,mousetarget)
			wait(0.5)
			debounce = false
		end
	end

(Untested).

this script works, but Im curious about what kind of disadvantages you were refering to?

Lets say that both parts in the image have CanCollide off.
image

The script will ignore the first part like it should. (The ignored part is represented by it being faded out).
image

The second part (which is also CanCollide = false) would not be ignored. So, the projectile will go off-course.

If you used the “folder method”, both parts will be ignored. Therefore, the projectile will be on target as expected.
image

I see, for now that shouldnt be too much of a problem for now but if needed I will try the ignorelist method.