UDim2

< Global namespace | Roblox namespace

A UDim2 is a Data Type composed of two UDims, one for the X coordinate and one for the Y coordinate, used to position and size GUIs.

Quick Reference[edit]

Constructors
SyntaxDescription
UDim2.new()Creates a new UDim2 whose values are all set to zero.
UDim2.new(number xScale, int xOffset, number yScale, int yOffset)Creates a new UDim2, using two pairs of UDim coordinates.
UDim2.new(UDim xDim, UDim yDim)Creates a new UDim2, by using two UDim coordinates.
Properties
SyntaxDescription
UDim Height The y-dimension scale and offset.
UDim Width The x-dimension scale and offset.
UDim X The x-dimension scale and offset.
UDim Y The y-dimension scale and offset.
Functions
SyntaxDescription
UDim2 UDim2:Lerp(UDim2 goal, number alpha)Returns a UDim2 interpolated between this, and the goal.
Operators
SyntaxDescription
UDim2 + UDim2Returns a new UDim2, with each component added individually.

Constructors[edit]

UDim2.new[edit]

UDim2 UDim2.new()

Description:
Creates a new UDim2 whose values are all set to zero.

UDim2 UDim2.new(number xScale, int xOffset, number yScale, int yOffset)

Description:
Creates a new UDim2, using two pairs of UDim coordinates.

UDim2 UDim2.new(UDim xDim, UDim yDim)

Description:
Creates a new UDim2, by using two UDim coordinates.

Properties[edit]

X[edit]

UDim UDim2.X [readonly]

Description:
The x-dimension scale and offset.

Y[edit]

UDim UDim2.Y [readonly]

Description:
The y-dimension scale and offset.

Width[edit]

UDim UDim2.Width [readonly]

Description:
The x-dimension scale and offset.

Height[edit]

UDim UDim2.Height [readonly]

Description:
The y-dimension scale and offset.

Methods[edit]

Lerp[edit]

UDim2 UDim2:Lerp(UDim2 goal, number alpha)

Description:
Returns a UDim2 interpolated between this, and the goal.

Notes:

  • alpha should be between 0 and 1.

Operators[edit]

UDim2 + UDim2[edit]

UDim2 UDim2 + UDim2

Description:
Returns a new UDim2, with each component added individually.

Examples[edit]

For the arrangement of guis below

  • Tree-collapse.png
    Frame
    Parent
    • TextLabel
      Child

The size of the inner GUI is calculated with

Child.AbsoluteSize.X == Child.Size.X.Offset
                      + Parent.AbsoluteSize.X * Child.Size.X.Scale
 
Child.AbsoluteSize.Y == Child.Size.Y.Offset
                      + Parent.AbsoluteSize.Y * Child.Size.Y.Scale

And the position of the inner GUI is calculated with

Child.AbsolutePosition.X == Parent.AbsolutePosition.X
                          + Child.Position.X.Offset
                          + Parent.AbsoluteSize.X * Child.Position.X.Scale
 
Child.AbsolutePosition.Y == Parent.AbsolutePosition.Y
                          + Child.Position.Y.Offset
                          + Parent.AbsoluteSize.Y * Child.Position.Y.Scale

UDim2 values are used only in GUI objects. This allows you to place objects on the player's screen exactly where you want them to go. Also, because the UDim2 scales according to it's parent's size, you can set up a GUI that will scale to a different screen size by using a ScreenGui or a Frame.

Here are a few code examples of objects placed using UDim2 locations. Remember that "zero" is the top left corner of a GUI object.

--A 50×50 frame 5 pixels from the top, 10 pixels from the left
mainFrame.Position = UDim2.new(0.0, 10, 0.0, 5)
mainFrame.Size = UDim2.new(0.0, 50, 0.0, 50)
--A frame that is half the size of the main screen, in the center

mainFrame.Position = UDim2.new(0.25, 0, 0.25, 0)

mainFrame.Size = UDim2.new(0.5, 0, 0.5, 0)
--A 50×50 frame in the top right corner

mainFrame.Position = UDim2.new(1.0, -50, 0.0, 0)

mainFrame.Size = UDim2.new(0, 50, 0, 50)
--A frame that is 1/4 the size of the screen, in the bottom right corner

mainFrame.Position = UDim2.new(0.75, 0, 0.75, 0)

mainFrame.Size = UDim2.new(0.25, 0, 0.25, 0)
--Combinations of Scale and Offset are acceptable too: A GUI that takes up half the screen and 10 more UDim2s
mainFrame.Size = UDim2.new(0.5, 10, 0.5, 10)

See also[edit]

Referenced by