Mouse hover on Gui is offset when resolution is changed

So basically there is a frame and when i hover on it a 2nd frame gets shown as visible and gets positioned at the mouse.

But when the resolution of the screen changes the 2nd frame is now offset and not positioned at the mouse.

local Player = game.Players.LocalPlayer
local PGui = Player:WaitForChild("PlayerGui")
local runservice = game:GetService("RunService")
local Mouse = Player:GetMouse()
local SHOWGUI = PGui.Ui.MoneyPerSec
local Frame = PGui.Ui.MoneyPerSec.MoneyPerSecHover

local hovering = false

SHOWGUI.MouseEnter:Connect(function()
	hovering = true
end)

SHOWGUI.MouseLeave:Connect(function()
	hovering = false
end)

while true do
	wait()
	if hovering == true then
		local X = Mouse.X
		local Y = Mouse.Y
		Frame.Visible = true
		Frame.Position = UDim2.new(0,X - 425,0,Y -20)
	else
		Frame.Visible = false
	end
end

The same issue happened with me where Roblox thinks the mouse is in a position that is like 30 pixels lower than it really is. I don’t know how it happens or how to fix it. I think it’s a bug maybe? Anyways, it probably doesn’t have to do with the script. My bad if I’m wrong.

It probably is the script.

I’ve seen roblox games that have solved this problem.

But thanks for the info.

2 Likes

The reason of this happening is that you have hard-coded pixel values that only work for a specific screen size(425, -20) instead if you want the entire thing to be dynamic, you should convert the mouse offset to scale and then add scale offsets instead of pixel ones:

while task.wait() do 
	Frame.Visible = hovering 
	if not hovering then continue end
	--Calculate scale from offset using the current screen size in pixels
	--Assuming PGui.Ui is the location of your ScreenGui
	local ScaleX = Mouse.X/PGui.Ui.AbsoluteSize.X
	local ScaleY = Mouse.Y/PGui.Ui.AbsoluteSize.Y
	--SXOffset and SYOffset should both be between -1 and 1
	--Configure them until they match the behavior you look for
	local SXOffset, SYOffset = 0, 0
	Frame.Position = UDim2.fromScale(ScaleX+SXOffset, ScaleY+SYOffset)
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.