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

Step 1 of 10

Open 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.

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.

Choose one

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

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

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 10

A 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
Work it out

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?

Link to this step · This confused me

Step 3 of 10

UDim2: 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
Ctrl Enter runs the cell. Checked against the expected output
Predict the output

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
A label is sized UDim2.new(0.5, -20, 0, 40) inside a frame 300 pixels wide. How wide is it?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 10

Let 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

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 the pairs

Match each instance to its job in the shop panel.

Link to this step · This confused me

Step 5 of 10

The 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)

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

Goal Resize the device until the shop panel touches the screen edge, then fix it with a UIScale clamp or a scale-based size.

The UDim2 phone: a preview of the simulation

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

Work it out

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

Link to this step · This confused me

Step 6 of 10

Where 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.

Find the broken line

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

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

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 7 of 10

How 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.
Choose all that apply

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

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

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 10

Build 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.

Link to this step · This confused me

Step 9 of 10

Challenges

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

Meet the criteria above, then record your own result. No test grades this check.

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

The Course Runner prints this code when the test passes.

Choose one

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

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

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 10 of 10

Recap

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

Link to this step · This confused me