Convert offset to scale

Hi
I’ve been looking through different posts but couldn’t get a straight forward answer. I want a function that would take x and y value as an input (gui object offset sizes) and return me a UDim2.new() object with those values switched into scale instead of offset.

Also I don’t use a plugin because the object in instanced by a script. And during this instancing process I want to set the size of it to the given x and y but as scale.

You’ll just want to divide the offset size by the actual bounds of the screen:

Scale = offsetInput / screenSize

Do that for X and Y and you’ll be able to convert whatever you want easily. However, the screen size must be defined so scale can be calculated and can scale properly with everything else as well

3 Likes

Thank you! Do you have an idea of how would I get the screenSize? Is it from the camera property maybe?

I tried doing something like this:

local vS = workspace.Camera.ViewportSize
return UDim2.new(x/vS.X, 0, y/vS.Y, 0)

inputs were x=300, y=200
but it just returned me UDim2.new(300, 0, 200, 0)

After some searches and help of Avxnt I got it.
Here is the function I made:

local function scale(x, y, screenGui)
	local absoluteSize = screenGui.AbsoluteSize
	return UDim2.new(x/absoluteSize.X, 0, y/absoluteSize.Y, 0)
end
7 Likes

What about Offset to Scale? I’m sure that will also come in handy.

1 Like