Converting Scale to Offset

I recently had the problem that I need to convert Scale to Offset position via a Script and I have no idea how to.
I already looked trough multiple topics, none which could answer my question.
So if someone has a small code that does that for me, I would apreciate it.

Yeah yeah no thats the point. I don’t need it for making / resizing the UI, I need the values for scripting purposes.

Oh sorry I misunderstood. --------

Yeah thats fine. The thing is, I have UI that is in Scale and a dragable UI which is in Offset and I need to be able to evaluate its relative position to the other UI.

I think this should help (Posts 2 and 4 both provide valid solutions)

I just saw this somewhere

local cam = workspace.Camera
local viewportSize = cam.ViewportSize

local function ScaleToOffset(x, y)
	x *= viewportSize.X
	y *= viewportSize.Y
	return x, y -- Only if necessary; you could change the frame itself
end

local function OffsetToScale(x, y)
	x /= viewportSize.X
	y /= viewportSize.Y
	return math.floor(x), math.floor(y) -- Only if necessary; you could change the frame itself
end
1 Like

@Lielmaster For what you posted I just input the 0.501 and 0.601 of “{0.501, 0},{0.601, 0}”, correct?
Cause if so, I have been getting incorrect results.

@wc3u I tried both solutions, which each gave me different but incorrect results.

yes,

ScaleToOffset(Scale X,Scale Y) -- Returns Offset of X and Y
OffsetToScale(Offset X,Offset Y) -- Returns Scale of X and Y

Edit: im not sure why you are getting incorrect results. are you sure you used the correct function? and also it should be in a local script since it gets the size of the screen

Edit2: converting scale to offset could return a number with decimal, I fixed the code for the Scale to Offset

If everything is giving you incorrect results, that might be because we aren’t understanding what exactly you intend to do. Do you want to convert scale to offset relative to the player’s screen, to a specific gui element, or something else entirely?

You mean using a script to convert the scale to offset? If so, can you elaborate?

Iam literally just trying to convert a Scale position into Offset

can you print the results and send it here and tell us what you expected to get

absolutesize/absoluteposition is offset

Correct result: 0,376 0,360
Your result: 787.572 465.174
https://csgo-russians.go-get-a.life/OyKobw

oh I think I know the problem, scale is based on the frames size, if you use offset it will base on pixels. Im assuming it is a child of a frame that has a different size

Yes, Its a child of a frame located within the GUI object.

Scale is technically just a percentage so a Gui with a size property of “0.3,0,0.9,0” is just
30% on the X axis
90% on the Y axis
(Of the whole screen)
So you would need:
P% * Axis = Offset
As a example say we have a 1920x1080 screen using the size above

X = Gui.Size.X.Scale * ScreenGui.AbsoluteSize.X
Y = Gui.Size.Y.Scale * ScreenGui.AbsoluteSize.Y
print(X, Y)
-- X = 576 and Y = 972

This is all assuming your Gui is a child of the screen gui
Edit: Just saw the reply saying its not a child of a screen gui 1 second I’ll think of something

@Lielmaster @starnova224 so what do I do now?
It has to stay in there since it’s basically a inventoryslot. The whole UI needs to stay somewhat organized in order for me to properly work with it.

This is the updated version.
parentFrame is the Size of the parent
I am gonna assume that your parentFrame is in Offset, if not then change it to offset. This may not 100% fix it

local cam = workspace.Camera
local viewportSize = cam.ViewportSize

local function ScaleToOffset(x, y, parentFrame)
	if parentFrame then
		x *= parentFrame.AbsoluteSize.X
		y *= parentFrame.AbsoluteSize.Y
	else
		x *= viewportSize.X
		y *= viewportSize.Y
	end
	return math.round(x), math.round(y)
end

local function OffsetToScale(x, y, parentFrame)
	if parentFrame then
		x /= parentFrame.AbsoluteSize.X
		y /= parentFrame.AbsoluteSize.Y
	else
		x /= viewportSize.X
		y /= viewportSize.Y
	end
	return x, y
end

Edit: I updated the script to use the Absolute Size of the parentFrame instead. The third parameter is now the parent object.
Edit2: The third parameter is the parent frame which is optional. The size returned is relative to the size of the given parentFrame or your screen size if parentFrame is not given.

Example
local frame = Instance.new'Frame'
frame.Size = UDim2.fromOffset(500,400)

local function OffsetToScale(x, y, parentFrame)
	if parentFrame then
		x /= parentFrame.AbsoluteSize.X
		y /= parentFrame.AbsoluteSize.Y
	else
		x /= viewportSize.X
		y /= viewportSize.Y
	end
	return x, y
end

print(OffsetToScale(100,300,frame)) -- 0.2, 0.75
-- It means that 100 is 20% of 500 which is the size of the parent frame in the x axis
-- and 300 is 75% of 400 which is the size in the y axis
8 Likes

Wait you’re making a inventory?
And I’m guessing you need the Gui’s to be a in grid layout?
If so use a “UIGridLayout
If you just need the offset of a Gui’s size I have now realized I believe “AbsoluteSize” is the offset of a gui