UserInputService Inputbegan detects clicks through frames

Hello!

So, I’ve been trying to get a frame to detect a click with InputBegan, and it works. When I have two frames with the same code that detect the click, it works. But, when both frames are overlapping, it registers a click on both. How do I only get the top frame to detect a click?

Any help is appreciated. Thank you,
Robyn

(This is a very similar topic to my prev. topic which never got answered. This may be in the wrong category as well.)

There are different solutions for this, but the one that I have personally used for situations like this is to use PlayerGui:GetGuiObjectsAtPosition(x, y) BasePlayerGui | Documentation - Roblox Creator Hub

This method conveniently orders in order of screen appearance, so the first valid UI in the list will be the one on top.

Thank you. Is this an intended studio feature or a bug? Also, are there any alternate solutions, as you mentioned there was more than one.

Intentional (very likely), there are situations in which you may have overlapping UI and want to register input on both.

You could look at ZIndex’s manually, or do something like storing a variable that will pick a frame as the top frame and only process that one if it is clicked or something, but those solutions are a little bit more jank than just looking at GetGuiObjectsAtPosition (race condition, more complex state handling for the logic for zero reason).

Ok, thank you. I really didn’t want to rewrite my entire script because of this one thing, but I guess I have to. I will rewrite the script and then mark you as a solution once it’s finished and there are no more flaws.

Is there anything other than UserInputService I can use to detect clicks that won’t register on the objects underneath? If not, I’ll just switch to using buttons.