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
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
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.
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
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
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
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!