How to find what gui the cursor is on

I need to find what gui the cursor is on, but can’t find a good way to do it.


I’ve tried GetGuiObjectsAtPosition and given it the mouse position from GetMouseLocation, but for some reason it doesn’t work (thinks the cursor is on something when it’s not & vice versa).

Also, I know of MouseEnter and MouseLeave functions but I’m using a lot of gui’s and would rather not create like 80 events just to do this one thing

1 Like

GetGuiObjectsAtPosition would return whatever GUI objects the mouse is over, whether it is transparent or not. Go through the objects and remove them from the table if its ScreenGUI isn’t enabled or the object is transparent.

1 Like

mouse enter and mouse leave could work, even though you dont want to make 80 events. you can just make a separate function that connects the mouse enter event and mouse leave, then use a for loop to connect all of them to the function you made

1 Like

I did a test where there were only 2 frames on screen (so none I couldn’t see) and the problem still occurs

or you could do like, a for loop, check if it is a ui object, then just connect it to mouse enter and mouse leave

for i, v in pairs(REPLACETHIS:GetDescendants()) do
	if v:IsA("Frame") then -- add more stuff
		v.MouseEnter:Connect(function()
			print(v);
		end);
		v.MouseLeave:Connect(function()
			print(v);
		end);
	end;
end;
1 Like

That would still create all the events though wouldn’t it? Even if they are connected in a loop

it would, yes, but it is easier then copy and pasting everything onto like 10000 lines

I don’t see any reason why :GetGuiObjectsAtPosition() would not work unless you are not taking into account the offset created from the GuiInset. You can offset this by doing something like this:

	local offset = GuiService:GetGuiInset()
	mousePosition = mousePosition - Vector2.new(0, offset.Y)
	return PlayerGui:GetGuiObjectsAtPosition(mousePosition)

Hope this helps!

3 Likes

Thanks, never would have thought of that
@clearifies thanks for helping too

2 Likes