GetGuiObjectsAtPosition not respecting the button position

I want to get the GUI button under the mouse.
For that, I’m using:

function MouseMove()
	local MousePos = UserInputService:GetMouseLocation() -- get X and Y of mouse on screen
	local GuiAtPosition = PlayerGui:GetGuiObjectsAtPosition(MousePos.X, MousePos.Y)
	print("GuiAtPosition: ", GuiAtPosition)
end

UserInputService.InputChanged:Connect(function(Input)
	if Input.UserInputType ==  Enum.UserInputType.MouseMovement then MouseMove() end
end)

However, for some reason, Studio is detecting the button as if it is shifted higher than it actually is.

For example, in this position of the mouse, it begins to detect the button much higher than where it is actually positioned:

… and moving the mouse down, it stops detecting the button much earlier:

What could be wrong?

UserInputService:GetMouseLocation doesn’t account for the GUI inset imposed by the topbar, so you will want to subtract it by the inset.

local MousePos = UserInputService:GetMouseLocation() - GuiService:GetGuiInset()
5 Likes