6 min read

Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.

UDim2

The UDim2 data type represents a two-dimensional value where each dimension is composed of a relative scale and an absolute offset in logical pixels. It is a combination of two UDim data types representing the X and Y dimensions. The most common usages for UDim2 are setting the Size and Position of GuiObjects.

local guiObject = Instance.new("Frame")
guiObject.Size = UDim2.new(0, 300, 1, 0)  -- 300 logical pixels wide; full height of parent
guiObject.Position = UDim2.new(0, 50, 0, 0)  -- 50 logical pixels from the left
print(guiObject.Size) --> {0, 300}, {1, 0}

Constructors

NameType / ReturnsDescription
UDim2.newReturns a new UDim2 with the coordinates of two zero UDim components representing each axis.
UDim2.newReturns a new UDim2 given the coordinates of the two UDim components representing each axis.
UDim2.newReturns a new UDim2 from the given UDim objects representing the X and Y dimensions, respectively.
UDim2.fromScaleReturns a new UDim2 with the given scale components and no offsets.
UDim2.fromOffsetReturns a new UDim2 with the given offset components and no scaling.

UDim2.new

Returns a new UDim2 with both dimensions set to zero — equivalent to UDim2.new(0, 0, 0, 0). All four components (X scale, X offset, Y scale, Y offset) default to 0.

UDim2.new

Returns a new UDim2 from four numbers that define both axes. Each pair of arguments defines one dimension: xScale and xOffset for the horizontal (X) axis, then yScale and yOffset for the vertical (Y) axis. Scale is a relative fraction of the parent container's size (typically between 0 and 1), while offset is an absolute logical pixel value added after scaling. All parameters default to 0.

-- 50% width + 20 logical pixels padding, full height
local size = UDim2.new(0.5, 20, 1, 0)

Parameters

NameTypeDefaultDescription
xScalenumber0The X dimension scale.
xOffsetnumber0The X dimension offset.
yScalenumber0The Y dimension scale.
yOffsetnumber0The Y dimension offset.

UDim2.new

Constructs a UDim2 by composing two UDim values directly. The first argument becomes the horizontal dimension (accessible via .X or .Width) and the second becomes the vertical dimension (accessible via .Y or .Height).

local x = UDim.new(0.5, 10)
local y = UDim.new(1, 0)
local size = UDim2.new(x, y)  -- equivalent to UDim2.new(0.5, 10, 1, 0)

Parameters

NameTypeDefaultDescription
xUDimThe UDim for the horizontal (X) axis.
yUDimThe UDim for the vertical (Y) axis.

UDim2.fromScale

Returns a new UDim2 with the given scalar coordinates and no offsets. Equivalent to:

local xScale, yScale = 0.5, 0.25
print(UDim2.fromScale(xScale, yScale) == UDim2.new(xScale, 0, yScale, 0)) --> true

Parameters

NameTypeDefaultDescription
xScalenumber0The relative scale for the horizontal (X) axis.
yScalenumber0The relative scale for the vertical (Y) axis.

UDim2.fromOffset

Returns a new UDim2 with the given offset coordinates and no scaling. Equivalent to:

local xOffset, yOffset = 100, 50
print(UDim2.fromOffset(xOffset, yOffset) == UDim2.new(0, xOffset, 0, yOffset)) --> true

Parameters

NameTypeDefaultDescription
xOffsetnumber0The absolute logical pixel offset for the horizontal (X) axis.
yOffsetnumber0The absolute logical pixel offset for the vertical (Y) axis.

Properties

NameType / ReturnsDescription
UDim2.XUDimThe X dimension scale and offset of the UDim2.
UDim2.YUDimThe Y dimension scale and offset of the UDim2.
UDim2.WidthUDimThe X dimension scale and offset of the UDim2.
UDim2.HeightUDimThe Y dimension scale and offset of the UDim2.

UDim2.X

The UDim component for the horizontal axis. The returned UDim has a .Scale (relative fraction) and .Offset (absolute logical pixels). This property is equivalent to UDim2.Width.

local pos = UDim2.new(0.5, 10, 1, 0)
print(pos.X.Scale)   -- 0.5
print(pos.X.Offset)  -- 10
FieldValue
typeUDim

UDim2.Y

The UDim component for the vertical axis. The returned UDim has a .Scale (relative fraction) and .Offset (absolute logical pixels). This property is equivalent to UDim2.Height.

local pos = UDim2.new(0.5, 10, 1, 0)
print(pos.Y.Scale)   -- 1
print(pos.Y.Offset)  -- 0
FieldValue
typeUDim

UDim2.Width

A synonym for UDim2.X. Returns the UDim component for the horizontal axis, containing both .Scale and .Offset.

FieldValue
typeUDim

UDim2.Height

A synonym for UDim2.Y. Returns the UDim component for the vertical axis, containing both .Scale and .Offset.

FieldValue
typeUDim

Methods

NameType / ReturnsDescription
UDim2:LerpUDim2Returns a UDim2 interpolated linearly between the value and the given goal.

UDim2:Lerp

Returns a UDim2 interpolated linearly between this UDim2 and the given goal. The alpha value should be a number between 0 and 1.

Parameters

NameTypeDefaultDescription
goalUDim2The target UDim2 to interpolate toward.
alphanumberA value between 0 (returns this UDim2) and 1 (returns goal) controlling the interpolation.

Returns

TypeDescription
UDim2A UDim2 where each axis is independently interpolated between this value and goal by the fraction alpha.

Operators

NameType / ReturnsDescription
+UDim2Produces a UDim2 with components that are the sum of the respective components of the two UDim2 objects.
-UDim2Produces a UDim2 with components that are the difference of the respective components of the two UDim2 objects.

UDim2 + UDim2 (addition)

Adds both the scale and offset of each axis independently: the result's X scale is the sum of both X scales, the result's X offset is the sum of both X offsets, and likewise for Y.

local a = UDim2.new(0.5, 10, 0, 20)
local b = UDim2.new(0.25, 5, 1, 0)
local c = a + b  -- UDim2.new(0.75, 15, 1, 20)
FieldValue
operation+
type aUDim2
type bUDim2
return typeUDim2

UDim2 - UDim2 (subtraction)

Subtracts both the scale and offset of each axis independently: the result's X scale is the first X scale minus the second, the result's X offset is the first X offset minus the second, and likewise for Y.

local a = UDim2.new(1, 100, 1, 50)
local b = UDim2.new(0.5, 30, 0, 10)
local c = a - b  -- UDim2.new(0.5, 70, 1, 40)
FieldValue
operation-
type aUDim2
type bUDim2
return typeUDim2