Is there a way for Mouse.Target to ignore something?

  1. What do you want to achieve?

I want Mouse.Target to ignore stuff

  1. What is the issue?

When I shoot something I don’t want some invisible wall to block the shot

  1. What solutions have you tried so far?

I have, but couldn’t find anything

local function Fire(Found)
	if Player.Status.IsDead.Value == true then return end
	if Player:FindFirstChild("CantFireTool") then return end
	local Target = Mouse.Target
	local Position = Mouse.Hit.p
	if CanFireTool == false then
		CanFireTool = true
		Weapon_Reloading = false
		Weapon_Reloading_Cancelled = true
		Events.FireTool:FireServer(Target, Position)
		addcond.Scope(ScopeInSetting.Default[1], false)
		ThirdPersonAnimations.Fire:Play()
		FirstPersonAnimations.Fire:Play()
		ThirdPersonAnimations.Reload:Stop()
		FirstPersonAnimations.Reload:Stop()
		local Wait_Time = ReplicatedStorage.Tools[Found.Name].FireRate.Value
		wait(Wait_Time)
		CanFireTool = false
	end
end

You can use Mouse | Roblox Creator Documentation to filter out a specific part. Just set it to the part that you want to exclude.

You could put your code in a if statement if your mouse.Target has a transparency of 1 or is locked or whatever you want it to be.
You could also use the Mouse.TargetFilter instance

  1. Yes, I have tried it but something like this occurs
  2. As for TargetFilter, the map gets cloned from the server storage, so I couldn’t do anything about that

Create a folder of the invisible walls, once you clone your map to your workspace add TargetFilter to that folder. I think this would work

2 Likes

Whenever I’m making a gun, I typically avoid using the mouse object altogether. Not that it’s bad, it’s just that there are better ways to get the world position of your mouse while ignoring one or more instances.

The mouse object only lets you set the TargetFilter to one singular instance, so what if you want to ignore multiple instances? Either you parent them all to one object on the client, which is risky if some of those objects are accessed by other client side code, or you can go about it a different way.

As opposed to using Mouse.Hit, which is less practical, you can get the on screen position of the mouse, and find the corresponding world position, while ignoring as many instances as you want.

local userInputService = game:GetService("UserInputService")

local v2 = userInputService:GetMouseLocation()
local ray = game.Workspace.CurrentCamera:ViewportPointToRay(v2.X, v2.Y)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {game.Workspace.InvisibleWall, game.Workspace.OtherObjectToIgnore}

local hitPosition
local raycastResult = game.Workspace:Raycast(ray.Origin, ray.Direction * 300, params)
if (raycastResult) then
     hitPosition = raycastResult.Position
else
     hitPosition = ray.Origin + ray.Direction * 300
end

print(hitPosition)

This bit of code will retrieve the Vector2 position of the mouse, which is comprised of two coordinates, the X and Y coordinate. This is just the on-screen position. In order to get the world position, we need to do a raycast. We can use the ViewportPointToRay function of the Camera object to get the information we need to raycast, which would be the origin of the ray, and the direction the ray is pointing in. Before using the Raycast function of Workspace, you can specify a RaycastParams, which is an object that contains preferences as to how the ray is to be cast. What we’re concerned about is its property called FilterDescendantsInstances, and as implied by its name, it’s a table that contains objects to be ignored by the raycast.

I’ve defined a variable called hitPosition and I’m setting it according to whether or not the raycast detects a hit. If it does detect a hit, set the variable to the position at which the raycast detected a hit, otherwise just set it to the position that is 300 studs from the camera in the direction the ray was cast.

I hope this has helped you, and that I haven’t gone into too much detail. I tend to get carried away. Happy new years.

3 Likes