Function that converts a UDim2 into AbsolutePosition

I first had this idea when I was experimenting with this tutorial to create a radar chart. This tutorial used AbsolutePosition (Vector2) for its math, but the way I wanted to make my radar chart, I would only have UDim2 values. Instead of the hacky method of placing a gui object on the UDim2 position and grabbing the AbsolutePosition from it, I decided to write this simple function that does it without creating any instances.

local function absolutepos(udim2)
	local absoluteSize = game:GetService("Workspace").CurrentCamera.ViewportSize
	local xOffset = udim2.X.Scale * absoluteSize.X
	xOffset += udim2.X.Offset
	local yOffset = udim2.Y.Scale * absoluteSize.Y
	yOffset += udim2.Y.Offset
	return Vector2.new(xOffset, yOffset)
end

There isn’t much to break down here, but I’ll do so anyway:
The absoluteSize variable represents the screen resolution of the client as a Vector2. Since the Scale value of any UDim object is based on screen resolution, we can simply multiply the scale by the screen res for each respective axis. From there, its as simple as adding the offset of the given UDim2 to the converted scale of the given UDim2 and returning it. Now any UDim2 can be used in terms of AbsolutePosition!

Although this may not seem very useful, it can be incredibly helpful when creating complex gui systems/charts that cannot easily use scale due to their math-heavy nature. Worth noting that since this converts UDim2 to Vector2, it can also be used for AbsoluteSize and other Vector2 properties.

7 Likes