Converting Scale to Offset

@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

The whole story is a bit more complicated. The person im making this for wants a drag and drop system within the inventory, and in order for me to correctly register the slot its dropped above I firstly have to register the slots positions in offset, since whenever the UI is dragged it changes its position to offset.

@Lielmaster I’m slowly starting to feel really stupid. X is not a valid member of Frame "Players.Flauschi_XD.PlayerGui.InventoryUI.Inventory"

I updated the script, try it again.
The reason it said this

is because you gave the Parent Object itself not the size. But dont worry I updated it, you can give the parent object

Correct result: 0,376 0,360
Your result: 376.08312341309 359.84449487305’

close enough, but damn

try use math.round after you got the results, or round the results before returning it from the function

Oh I see
“AbsolutePosition” and “AbsoluteSize” are what you’re looking for then
Also if the decimals are a problem use:

math.round(num)

It will round up if the decimal is higher than .5 and down if its less

Okay, thanks for all the help dude.
Much apreciated, especially the time you put into it.

Also, @starnova224 thanks for your attempt to help.