Lesson 1 of 6

UI fundamentals: ScreenGui, UDim2, layouts

Learn Roblox UI from Beacon Yard's real HUD and shop, from ScreenGui and DisplayOrder to UDim2 scale and offset, list layouts, UIScale and phone safe areas.

Concept lesson, about 60 minutes, 10 steps

By the end you can

  • Explain where a ScreenGui lives, who can see it, and how DisplayOrder stacks the HUD under the shop
  • Read and write UDim2 sizes and positions in scale and offset, and predict their pixel size on a given screen
  • Lay out a panel with UIListLayout, UIPadding and UIScale, and keep it inside the safe area on a phone

Before this: Remotes: events, functions, unreliable, buffers, Input everywhere, part 1: keyboard, gamepad, touch and accessibility

1Open the shop and look closely

Walk up to the quartermaster in Beacon Yard and hold the prompt. A dark panel with a gold edge appears in the middle of the screen: a title, three coloured buttons and a Close button. The HUD in the corner is still there underneath it. Everything you see on that screen is one kind of thing: a GUI object inside a ScreenGuiScreenGuiA container that draws 2D interface objects over one player's view of the game.Think of it as a sheet of glass in front of one player's screen..

Roblox Studio
Beacon Yard in a Studio playtest with the Quartermaster shop panel open in the centre, showing Speed, Shield, Coin Pack and Close buttons, and the coins HUD in the top-left corner.

Select a numbered pin, or a row below, to read what it marks.

Figure 1.1.

The shop over the HUD in a playtest. Select the pins to see which line of code made each part.

Both panels are built in code, in src/client/ShopGui.luau and src/client/Hud.luau. Neither one exists in the place file. A LocalScript creates them for one player when that player joins.

Two players are in the yard. Player A opens the shop. What does player B see?

  1. Nothing changes on B's screen; A's ScreenGui exists only on A's device
  2. The same panel, because GUIs replicate like parts
  3. A smaller copy of the panel in B's corner
  4. The panel, but greyed out
Answer

Nothing changes on B's screen; A's ScreenGui exists only on A's device. ShopGui runs in a LocalScript on each player's own device and parents the ScreenGui to that player's PlayerGui. Nothing about it is sent to anyone else.

Open this step in lesson mode · Link to this step · This confused me

2A screen that belongs to one player

A ScreenGui is a container that draws 2D objects on top of the 3D world. All of it is stored and changed on the client1. You can make one in two ways. Put it in StarterGui and Roblox copies it into each player's PlayerGui when they spawn. Or create it from a LocalScript and parent it to PlayerGui yourself, which is what Beacon Yard does.

The shop is drawn on one player's screen at home. The server never sees it. When a player presses Buy, the button is the controller: it can only send a request to the arena, and the server decides.

Think of it as Your screen and controller at home

Client sideRemember: The client is your screen and controller at home.

Two settings matter on every ScreenGui. ResetOnSpawn = false keeps it alive when the character respawns, since the LocalScript that built it does not run again. DisplayOrder stacks ScreenGuis: higher draws on top. The HUD uses 10 and the shop uses 20.

	screen.ResetOnSpawn = false
	screen.DisplayOrder = 20
	screen.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
	screen.IgnoreGuiInset = false
Figure 2.1

The HUD's ScreenGui has DisplayOrder 10. What is the smallest whole-number DisplayOrder a new settings menu needs so it always draws above the shop?

Answer

21. The shop is 20. Anything at 21 or more draws above it. Do not rely on the order of two ScreenGuis with the same value, so 20 is not enough.

Open this step in lesson mode · Link to this step · This confused me

3UDim2: a fraction plus some pixels

Every GUI object has a Size and a Position, and both are a UDim2UDim2A 2D size or position made of a scale (a fraction of the parent) and an offset (pixels) on each axis.Think of it as half the table, plus two fingers.. A UDim2 holds two numbers per axis: a scale, which is a fraction of the parent's size, and an offset, which is a number of pixels added on top2. UDim2.new(1, 0, 0, 84) means "full parent width, plus nothing; zero parent height, plus 84 pixels". That is exactly how every shop button is sized.

UDim2.fromScale(0.5, 0.5) and UDim2.fromOffset(400, 480) are shortcuts for "scale only" and "offset only". The shop panel uses both: its position is half the screen in each direction, its size is a fixed 400 by 480 pixels, and AnchorPoint = Vector2.new(0.5, 0.5) makes the position refer to the panel's centre instead of its top-left corner.

Luau
-- A UDim2 axis is scale * parentSize + offset.
local function axis(scale, offset, parent)
	return scale * parent + offset
end
 
-- A shop button: UDim2.new(1, 0, 0, 84) inside a panel
-- whose inner width is 400 - 18 - 18 pixels of padding.
local inner = 400 - 18 - 18
print("button width " .. axis(1, 0, inner))
print("button height " .. axis(0, 84, 480))
 
-- The panel's centre on a 1280 x 720 screen.
print("centre x " .. axis(0.5, 0, 1280))
Figure 3.1. Expected output: button width 364 button height 84 centre x 640

A label is sized UDim2.new(0.5, -20, 0, 40) inside a frame 300 pixels wide. How wide is it?

label.Size = UDim2.new(0.5, -20, 0, 40)
-- parent frame is 300 x 200 pixels
  1. 130 pixels
  2. 150 pixels
  3. 170 pixels
  4. 280 pixels
Answer

130 pixels. 0.5 times 300 is 150, and the -20 offset takes it to 130. Negative offsets are a common way to leave a gap.

Open this step in lesson mode · Link to this step · This confused me

4Let a layout place the children

Placing every child by hand breaks the moment you add one. A UI layoutUI layoutAn invisible child such as UIListLayout or UIGridLayout that positions its sibling GUI objects for you.Think of it as a shelf with fixed gaps: you add items and they line up. object does it for you. Put a UIListLayout inside a frame and it stacks every sibling in a row or a column, in LayoutOrder order, with a fixed gap3. A UIPadding keeps the children away from the frame's edges. The shop uses both, which is why its buttons have no Position at all.

	padding.PaddingTop = UDim.new(0, 18)
	padding.PaddingBottom = UDim.new(0, 18)
	padding.PaddingLeft = UDim.new(0, 18)
	padding.PaddingRight = UDim.new(0, 18)
	padding.Parent = panel
 
	local layout = Instance.new("UIListLayout")
	layout.FillDirection = Enum.FillDirection.Vertical
	layout.HorizontalAlignment = Enum.HorizontalAlignment.Center
	layout.SortOrder = Enum.SortOrder.LayoutOrder
	layout.Padding = UDim.new(0, 12)
	layout.Parent = panel
Figure 4.1

At m06 the order of the buttons comes from LayoutOrder: Title 1, Speed 2, Shield 3, Coin Pack 4, Close 5. Change Shield to 6 and it drops to the bottom. Other modifiers do similar jobs. UIGridLayout makes a grid of equal cells, UIFlexItem lets one child grow or shrink to fill the leftover space, and UIAspectRatioConstraint keeps a frame's shape when its size is in scale.

Match each instance to its job in the shop panel.

  1. UIListLayout
  2. UIPadding
  3. UICorner
  4. UIStroke
Answer

UIListLayout: Stacks the buttons in a column with a 12 pixel gap; UIPadding: Keeps 18 pixels between the edge and the content; UICorner: Rounds the corners of the panel and the buttons; UIStroke: Draws the gold outline around the panel. These are modifiers: invisible children that change their parent or arrange its siblings. None of them draw anything on their own except UIStroke's outline.

Open this step in lesson mode · Link to this step · This confused me

5The same panel on a phone

A panel that is 400 by 480 pixels fills a third of a laptop screen and most of a phone held sideways. Beacon Yard solves this with one shared module, UiScale, that adds a UIScale to each ScreenGui. A UIScale multiplies the size of everything under it, children and strokes included4.

	local function apply(camera: Camera)
		local size = camera.ViewportSize
		if size.X <= 0 or size.Y <= 0 then
			return
		end
		local shorter = math.min(size.X, size.Y)
		uiScale.Scale = math.clamp(shorter / 720, 0.65, 1.4)
Figure 5.1

The module divides the shorter side of the screen by 720 and keeps the result between 0.65 and 1.4. Phones also have notches, rounded corners and the Roblox top bar. ScreenInsets on a ScreenGui decides whether your UI stays inside that safe area, and IgnoreGuiInset = false, which both panels use, keeps them below the top bar.

The UDim2 phone

The UDim2 phone: a still of the simulation
Figure 5.2.

A phone frame you can resize. Pick Scale, Offset or UIScale for the shop panel and watch where each one breaks.

A phone's screen is 844 by 390 pixels in landscape. What UIScale does UiScale.attach set? Round to two decimals.

Answer

0.65 (within 0.005). 390 / 720 is about 0.54, which is below the 0.65 floor, so the clamp sets 0.65. The shop then draws at 260 by 312 pixels.

Open this step in lesson mode · Link to this step · This confused me

6Where the screen picture breaks

Where does it break?“Your screen and controller at home” is a good picture of the client. Where does it stop being true?

It breaks here. The client also simulates your own character's movement and sends it up, so it is more than a screen.

The true model. Each player's device runs its own copy of the game for display and input. It owns its character's physics by default, which is why movement feels instant and why a cheater can move strangely.

A screen at home only shows things. But the client in Roblox runs code: it builds the GUI, reads clicks and can change any of it. An exploiter can make the Speed button say "free" or fire ShopBuy a hundred times a second. That is why the button only sends a request, and why ShopService on the server checks the price and the balance.

This is a beginner's version of the Speed button. Which line is the bug?

button.Activated:Connect(function()
    local coins = player.leaderstats.Coins
    coins.Value -= 25
    shopBuy:FireServer("Speed")
end)
Answer

Line 3: coins.Value -= 25. Fixed: -- remove it: the server's trySpend takes the coins. Line 3 subtracts coins on the client. That change never reaches the server, so the player's screen and the real balance disagree. In Beacon Yard the button only fires ShopBuy, and DataService.trySpend takes the coins on the server.

Open this step in lesson mode · Link to this step · This confused me

7How studios build UI

Beacon Yard builds its UI with Instance.new so every property is in a file you can read and diff. That is one of several common ways, and studios mix them.

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
Studio's UI toolsDraw frames and buttons in the viewport, set properties in the Properties window, save them in StarterGui.Static menus, fast iteration with a designer, learning what each property does.The layout lives in the place file, so Git cannot diff it unless you export it as a model.
Instance.new in LuauBuild the tree in a LocalScript, as ShopGui and Hud do.Small UIs in a code-first repo, UI generated from data.Verbose, and every state change is a manual property write you must not forget.
A UI frameworkReact-lua, Fusion or Vide describe the UI as a function of state.Large UIs with many screens, lists and state, and teams that share components.A dependency to learn and update. The next lesson compares them.
UI stylingStyleSheets with rules, tokens and themes, edited in the Style Editor.Shared colours and fonts across many screens, light and dark themes.Newer engine feature; teams still settle on conventions.

Which of these belong in client UI code in Beacon Yard?

  1. Showing the prices from Config on the buttons
  2. Hiding the panel when Close is pressed
  3. Subtracting 25 coins when Speed is pressed
  4. Firing ShopBuy with the item name
  5. Deciding whether the player can afford Shield
Answer

Showing the prices from Config on the buttons; Hiding the panel when Close is pressed; Firing ShopBuy with the item name. Prices on labels, closing a panel and sending a request are all display or input. Taking coins and checking the balance are the server's job, in ShopService and DataService.

Open this step in lesson mode · Link to this step · This confused me

8Build along: a coins pill that fits a phone

You will add a small, empty coins pill to the top-right corner (putting the coin count inside it is wiring for later; here you practise sizing), sized in scale so it grows with the screen, and check it in the device emulator. Work on your own branch of the game repo. The game's own next step, the m07-feel milestone, polishes the shop instead: it pops in with a tween, shows your coins and dims what you cannot afford. Compare with git diff m06-drone-wrench m07-feel -- src/client/ShopGui.luau when you are done.

Build along

m06-drone-wrench

  • src/client/Hud.luau
  1. Check out m06-drone-wrench, run rojo serve, open the place and connect the Rojo plugin.

    You should see Studio opens the place and the Rojo plugin says Connected.

  2. In Hud.start, after the panel, create a Frame named CoinsPill with AnchorPoint = Vector2.new(1, 0), Position = UDim2.new(1, -16, 0, 16) and Size = UDim2.fromScale(0.2, 0.08). Add a UIStroke to it with Color = Color3.fromRGB(255, 200, 40) and Thickness = 2 for the yellow edge. Parent the frame to screen. Press Play.

    You should see A yellow-edged empty frame appears in the top-right corner during Play, about a fifth of the screen wide.

  3. Add a UIAspectRatioConstraint with AspectRatio = 3 to the pill. In the Test tab, open the device emulator and switch between a laptop and a phone in landscape.

    You should see The pill keeps the same shape when you switch devices in the emulator. At 1280 by 720 it narrows to about 173 pixels wide, roughly 13% of the screen, because the aspect ratio caps the width at three times the height.

  4. Check that the HUD ScreenGui has IgnoreGuiInset = false and ScreenInsets = Enum.ScreenInsets.CoreUISafeInsets (the default when IgnoreGuiInset is false), and check the phone again.

    You should see The pill sits fully inside the screen on the phone, clear of the notch and the top bar.

Open this step in lesson mode · Link to this step · This confused me

9Challenges

Level 1, Apprentice

Challenge

Change the shop panel from UDim2.fromOffset(400, 480) to a size in scale that matches on a laptop and still fits on a phone.

Start from
The game at milestone m06 (download)

Done means

  • The shop panel is sized in scale, not offset
  • On a 1280 by 720 window the panel is still about 400 by 480 pixels
  • On a phone in the emulator the panel fits on screen

Hints

  1. 400 divided by 1280 is the X scale you need.
  2. A UIAspectRatioConstraint keeps the panel from stretching on wide screens.
  3. UiScale still multiplies the result; check both together.

Level 3, Professional

Challenge

Start from m14-ship. Add a music mute toggle to the existing BeaconSettings accessibility panel in src/client/SettingsGui.luau. The panel already draws above the shop. Have the button write and read LocalPlayer's MusicMuted attribute.

Press Play and run this in the client command bar. Copy the BY- code from PASS CODE: in Output when the checks pass.

luau
require(game.ReplicatedStorage.GameTests.RunChallenge)("ui", "ui-settings-menu")
Start from
The game at milestone m14 (download)
Grade it
In Studio, press Play, then run require(game.ReplicatedStorage.GameTests.RunChallenge)("ui", "ui-settings-menu") in the client command bar. Copy the pass code from Output.

Done means

  • A ScreenGui named BeaconSettings lives in LocalPlayer.PlayerGui with ResetOnSpawn = false and DisplayOrder > 20 (above the shop)
  • BeaconSettings contains a UIListLayout (or UIGridLayout) and a TextButton named MuteButton; use a Frame for tidy layout (self-check: the test accepts any layout under the gui)
  • The MusicMuted boolean attribute on LocalPlayer is what the UI follows, and MuteButton.Text updates whenever it changes (self-check: the test sets the attribute directly, so clicking the button is yours to try in Play)
  • challenges/ui.luau, run on the client, prints TEST PASS for ui_settings_gui, ui_settings_button and ui_settings_toggle

Hints

  1. Add MuteButton after the existing accessibility toggles in src/client/SettingsGui.luau. Keep the existing ScreenGui and its start() call.
  2. Use LocalPlayer:GetAttributeChangedSignal("MusicMuted") to refresh MuteButton.Text (for example "Music: Off" when true and "Music: On" when false).
  3. This challenge is client-only on purpose: Module 6 will teach DataStores so the choice survives a rejoin.

Why does the client-only settings challenge run on the client instead of the server?

  1. BeaconSettings lives in LocalPlayer.PlayerGui on the client, and a client-set attribute stays on that client
  2. The server cannot read player attributes
  3. ScreenGuis only work in the command bar
  4. TextButtons replicate their clicks to the server automatically
Answer

BeaconSettings lives in LocalPlayer.PlayerGui on the client, and a client-set attribute stays on that client. LocalScript UI lives in LocalPlayer.PlayerGui on the client, and setting an attribute locally only updates that client. Saving a setting across sessions needs a server remote and a DataStore, which Module 6 adds.

Open this step in lesson mode · Link to this step · This confused me

10Recap

Recap

  1. A ScreenGui lives in one player's PlayerGui; DisplayOrder stacks the HUD (10) under the shop (20).
  2. UDim2 is scale times the parent plus an offset in pixels; AnchorPoint picks which point of the object the position refers to.
  3. Layouts, padding and one UIScale clamped between 0.65 and 1.4 keep the same panel usable from a phone to a monitor.

The client is your screen and controller at home, until the client also simulates your own character's movement and sends it up, so it is more than a screen.

Next UI design: Figma to Studio, hierarchy, accessibility

Open this step in lesson mode · Link to this step · This confused me