GetMouseLocation and GetGuiObjectsAtPosition not behaving as expected?

Honestly, not sure if this is just a silly mistake here, but I’m trying to make a tooltip when the mouse is hovered over different UI elements. It mostly works perfectly, however it seems that the script is assuming the UI is 30-50 pixels higher than it actually is. The X axis is working accurately, but I’m getting the tooltip triggered way too high like this:

and then the tooltip is being deactivated too early similarly like this:

This is my code below, assume the variables are all correct, is it not behaving correctly, or am I missing something?

local function UpdateTooltip()
	if Input.MouseEnabled then
		local MouseLocation = Input:GetMouseLocation()
		local Elements = PlayerGui:GetGuiObjectsAtPosition(MouseLocation.X, MouseLocation.Y)
		if Elements and #Elements > 0 then
			local OnScreen
			for _, Element in pairs(Elements) do
				if Element:FindFirstChild("Tooltip") and Element.Name ~= "Tooltip" then
					if OnScreen then
						if OnScreen.ZIndex < Element.ZIndex then
							OnScreen = Element
						end
					else
						OnScreen = Element
					end
				end
			end
			if OnScreen then
				Tooltip.Text = OnScreen.Tooltip.Value
				Tooltip.Visible = true
			else
				Tooltip.Text = ""
				Tooltip.Visible = false
			end
			Tooltip.Size = UDim2.new(0, Tooltip.TextBounds.X + 10, 0, 30)
			local ScreenSize = UI.AbsoluteSize
			local X = ((ScreenSize.X / 2) > MouseLocation.X and MouseLocation.X + 20) or (MouseLocation.X - Tooltip.TextBounds.X - 10)
			local Y = ((ScreenSize.Y - MouseLocation.Y) > 30 and (MouseLocation.Y - 20)) or (MouseLocation.Y - 55)
			Tooltip.Position = UDim2.new(0, X, 0, Y)
		else
			Tooltip.Text = ""
			Tooltip.Visible = false
		end
	else
		Tooltip.Text = ""
		Tooltip.Visible = false
	end
end
1 Like

IgnoreGuiInset is disabled? if it is, activate it.

3 Likes