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)
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)
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.
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.
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
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.
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