How to reformat :GetMouseLocation() to output scale?

I currently have a hover script, where it should make an info gui pop up when being hovered over, and it should follow the mouse, with a slight offset. But it really does not work with different screen sizes, this is the snippet of my script that does this.

	while hovering do
		wait()
		local mousePos = game:GetService("UserInputService"):GetMouseLocation()
		script.Parent.Position = UDim2.new(0, (mousePos.X - 550), 0, (mousePos.Y + -150))
	end

example

6 Likes

I would do something like

local UISize = script.Parent:GetAbsoluteSize()
local OffsetX = -(UISize.X/2 + 12)
local OffsetY = -(UISize.Y/4)

local mousePos = game:GetService("UserInputService"):GetMouseLocation()
script.Parent.Position = UDim2.new(0, (mousePos.X + OffsetX), 0, (mousePos.Y + OffsetY))

This uses the size of the pop up UI to determine the offset. You could maybe use scale, but you would need to know the scale of the pop up UI, which I don’t know and I think is less ideal

You should also consider replacing wait() with RunService.RenderStepped:Wait(), this will remove the choppy feeling of your pop up ui

hmm…

Hmm, that is odd
Is script.Parent the same size as, well, what can be seen?
What is the AnchorPoint of script.Parent?

You might also want to print UISize or the other offset values to debug this

Does this happen when you resize the window? If UISize is inside the while loop, then this should not be an issue

btw the script is inside the popup if that helps.

the anchor point is 0,0.

there is also an aspect ratio constraint, may be bugging it out.

yes it does happen when i resize the window, the whole point is to get it to look the same on all devices.

That should not happen… Can you show me a screenshot of the updated code? The local UISize = script.Parent:GetAbsoluteSize() should be inside the while loop.
The code I wrote basically uses the size of the UI element to offset it. This means that as the size of the pop up changes (when the screen size changes), the offset will also change. It is possible to update the offest only when the size of pop up changes (or when the size of the screen gui changes), but for the sake of simplicity, it is ran inside the loop

:GetAbsoluteSize() retrieves the “absolute” size (ie, how many pixels it takes up on the screen), so the aspect ratio should not affect it

OffsetX might need to be changed to local OffsetX = -(UISize.X + 12)

It might be better to use .AbsoluteSize instead of :GetAbsoluteSize(). I actually cannot find this method in the documentation so I might have invented it… Though I think it exists???

No, I think I invented it… Does the script error? It should not work if this method is invalid

I used .AbsoluteSize in the first place, so dw. I changed the OffsetX to -(UISize.X + 12) and now it looks like this, with and without resizing the window, so just needs alittle offset tinkering and im sure it will be just fine.


1 Like

my bad, APPARENTLY, it still doesnt completely work. It works on the Y axis though.



P.S. My mouse in the IPhone 14 screenshot was around the top middle of the popup.

Could you send a screenshot of the current code and the explorer structure of the pop up ui?

snippet of code

		game:GetService("RunService").RenderStepped:Wait()
		local UISize = script.Parent.AbsoluteSize
		local OffsetX = -(UISize.X + 12)
		local OffsetY = -(UISize.Y/4)

		local mousePos = game:GetService("UserInputService"):GetMouseLocation()
		script.Parent.Position = UDim2.new(0, ((mousePos.X + OffsetX) - 85), 0, (mousePos.Y + OffsetY))

explorer
image

Is your goal something like this?

If so this is what i did

local InfoFrame = script.Parent.Frame
local ToolTip = InfoFrame.Frame

local UserInputService = game:GetService('UserInputService')
local RunService = game:GetService('RunService')

local function update()
	local location = UserInputService:GetMouseLocation()
	local x, y = location.X, location.Y
	
	local screen = InfoFrame.AbsoluteSize
	
	local newX = x / screen.X
	local newY = y / screen.Y
	
	ToolTip.Position = UDim2.fromScale(newX + 0.01, newY - 0.04)
end

RunService.RenderStepped:Connect(update)

((mousePos.X + OffsetX) - 85)
The -85 might be what is causing issues for you

-(UISize.X + 12)
Here, what I was trying to achieve is having a fixed offset of 12 pixels between the UI and the mouse (thus why the 12). This offset should not be set too high as it doesn’t scale. You could also get rid of it entirely, but for padding I like using a fixed offset

If you want to modify it further, you can divide or multiply the UISize.X, but try to keep the fixed offset to a small value. How I wrote it, it is expected to be to the left of the mouse, with 12 pixels between the UI and the mouse

local OffsetX = 12
local OffsetY = 25

local location = UserInputService:GetMouseLocation()
local x, y = location.X, location.Y
local screen = InfoFrame.AbsoluteSize

local newX = (x / screen.X) + (OffsetX / screen.X)
local newY = (y / screen.Y) + (OffsetY / screen.Y)

this would convert the offset to scale

I don’t really like this approach because you still have to get the scale of the UI to move it to the left by 1 length of the UI. Using offset does the same thing, but simpler in my opinion.

You don’t really run into this issue in your video since the UI is to the bottom right, the mouse being where the anchor point it.
But actually, even simpler than scale and offset, would be to just change the AnchorPoint to (1,.5), and adding an offset of a couple pixels, and you are good to go

sry for the late reply,

where is this script located?
what are the info frames and tooltip frames?

That was an example I made to show you,
This is what you would do to make it into scale instead of offset

local OffsetX = 12
local OffsetY = 25

local location = UserInputService:GetMouseLocation()
local x, y = location.X, location.Y
local screen = InfoFrame.AbsoluteSize

local newX = (x / screen.X) + (OffsetX / screen.X)
local newY = (y / screen.Y) + (OffsetY / screen.Y)
	
ToolTip.Position = UDim2.fromScale(newX + 0.01, newY - 0.04)


ToolTip would be the tool tip when you are hovering over a unit, InfoFrame would be the Frame the Tooltip is in, which would be size 1, 0, 1, 0 and parented to the screen gui, not the unit frame

and just mess with the OffsetX and OffsetY until it is at a offset from the mouse that you like

this is the outcome, doesn’t really follow the mouse, its alittle slow

	while hovering do
		game:GetService("RunService").RenderStepped:Wait()
		script.Parent.Visible = true --tool tip
		local OffsetX = -200
		local OffsetY = 25

		local location = game.UserInputService:GetMouseLocation()
		local x, y = location.X, location.Y
		local screen = script.Parent.Parent.Parent.Parent.AbsoluteSize --screengui

		local newX = (x / screen.X) + (OffsetX / screen.X)
		local newY = (y / screen.Y) + (OffsetY / screen.Y)

		script.Parent.Position = UDim2.fromScale(newX + 0.01, newY - 0.04)
	end

ok I fixed it using @Tomi1231’s code, I was adding an offset not through the offset variable, so it was offsetting itself normally. It probably would’ve worked with the other code but this worked first. Thx for all the help!

1 Like

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