Roblox Wiki
Roblox Wiki
Advertisement
Roblox Wiki

The UDim2 data type represents the position and size of 2D GUI elements. Each dimension is composed of a relative scale (as per parent) and an absolute offset (as per unit), which is represented by UDim. UDim2 is constructed from number values or UDim values.

Constructors[]

UDim2.new(): UDim2
Returns a UDim2 value with all components set to zero.
UDim2.new(xScale: number, xOffset: number, yScale: number, yOffset: number): UDim2
Returns a UDim2 value with the given coordinates.
UDim2.new(x: UDim, y: UDim): UDim2
Returns a UDim2 value from the given UDim values.
UDim2.fromScale(xScale: number, yScale: number): UDim2
Returns a UDim2 value with the given scales and no offsets.
UDim2.fromOffset(xOffset: number, yOffset: number): UDim2
Returns a UDim2 value with the given offsets and no scales.

Members[]

X.Scale and Y.Scale set the size or position of an element proportionally to the size of its parent. A value of 1 will set the child to the same size or position as its parent on the specified axis, and 0 will make it have a size 0% of its parent, and have a position 0% of its parent on the specified axis.

X.Offset and Y.Offset set the size or position of an element in pixels. This number is added to the Scale parameters, after their size has been calculated: these values are offsets.

The size of a GUI is calculated using the formula:

Size = Scale*Parent.AbsoluteSize + Offset

The position of a GUI is calculated using the formula:

Position = Parent.AbsolutePosition + Scale*Parent.AbsoluteSize + Offset

Example[]

local frame = Instance.new("Frame")
frame.Position = UDim2.new(0.5, -50, 1, -100)
frame.Size = UDim2.new(0, 100, 0, 100)
frame.Parent = GUI

This creates a 100×100 pixel rectangle, centered at the bottom of the screen.

Referenced by[]


External links[]


Advertisement