Getting the mouse position and turning it into a UDim2 with screen position and not offset

Hi! When you have a frame and have another frame inside of it, the screen size 1,1 is the size of the first frame. I want to get the position of the players mouse and turn it into a UDim2 with the screen position. Is there any way of doing that? Thank you.

Do you mean you want to change it to a UDim2 using the Scale instead of offset?
If so, you can just divide the mouse position by the ViewSize. For example:

mouse.X / mouse.ViewSizeX

That will give you the X scale. Likewise, you can do the same with the Y axis. And, I’m pretty sure from there you know how to get it into a UDim2.

Is it possible to have it not have the position for the entire screen but in a area of it? like the area inside of a GUI. Sorry if you don’t understand what I’m saying. I am not very good at explaining things.

If you want to get the position of the mouse in a frame you can do:

localscript:

local Player = game:GetService("Players").LocalPlayer
local Mouse  = Player:GetMouse()

local OuterFrame = path.to.outerframe
local InnerFrame = path.to.innerframe

local MousePosition = Vector2.new(Mouse.X, Mouse.Y) - OuterFrame.AbsolutePosition	
InnerFrame.Position = UDim2.fromOffset(MousePosition.X, MousePosition.Y)

Is it possible to use scale instead of offset?

Yes! It’s just like what kyle said,

But instead to convert from offset to scale we instead of dividing the position with the entire screen,
we divide with the outerFrame absolute size!

Heres the script:

local Player = game:GetService("Players").LocalPlayer
local Mouse  = Player:GetMouse()

local OuterFrame = path.to.outerframe
local InnerFrame = path.to.innerframe
	
local MousePosition = (Vector2.new(Mouse.X, Mouse.Y) - OuterFrame.AbsolutePosition ) / OuterFrame.AbsoluteSize	
InnerFrame.Position = UDim2.fromScale(MousePosition.X, MousePosition.Y)

Thank you! I will try it to see if it works.

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