Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make your mouse ignore a brick?

I know about targetfilter but its not working for some reason. Basically I have a clickdetonator brick inside of a regular but bigger brick. I want to be able to make my mouse ignore the bigger brick in order to click the clickdetonator brick. Here is my code so far using targetfilter.

player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.Pieces

1 answer

✓ Accepted answer

Use ScreenPointToRay. There's a workspace function named :FindPartOnRayWithWhitelist which allows the ray to pass through all objects that aren't in the 2nd argument.

https://developer.roblox.com/api-reference/function/Camera/ScreenPointToRay

mouse.TargetFilter will only work if the mouse target is equal to the filter. Therefor, we can't use it to pass through objects.

local UserInputService = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera

UserInputService.InputBegan:Connect(function(InputObject) --you should also use GameHandledEvent to check if the client clicks on a Gui element
	if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		local Direction = InputObject.Position
		local CPoint = Camera:ScreenPointToRay(Direction.X, Direction.Y)
		local NewRay = Ray.new(CPoint.Origin, CPoint.Direction * 5000)

		local Target = workspace:FindPartOnRayWithWhitelist(NewRay, {workspace.Pieces})	 --the way will only hit this and its descendencies.	
		if Target then
			print(Target.Name) --good to go
		end
	end
end)

6 comments

0

Ahh I figured it was something like that, I'll read up on this. Appreciate it man

0

You can also use the clients mouse (x & y) to create the ray, however, I think ScreenPointToRay offers more and it's good practice to use it instead.

0

Yeah I'm not that experienced with rays in general so this offers me a good chance to learn something new xD

0

Hmm doesnt seem to work for me. It's still unable to pass through the part in order to click the clickdetonator. Instead its just printing the target.Name which matched a brick in the model pieces. Weird

0

Yes, that's what the code does. Is that not what you were trying to do? Is ClickDetonator a part?

0

Yeah I get how it works but the thing is I wanted to be able to click the clickdetonator brick when i click an accessory brick above it. It's fine though I can just this method and recode it to fit xD

This discussion is preserved as it was. New answers, comments, and votes are closed.