✓ 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)
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})
if Target then
print(Target.Name)
end
end
end)
6 comments
Ahh I figured it was something like that, I'll read up on this. Appreciate it man
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.
Yeah I'm not that experienced with rays in general so this offers me a good chance to learn something new xD
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
Yes, that's what the code does. Is that not what you were trying to do? Is ClickDetonator a part?
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