Guide
How Roblox GUI code works
A ScreenGui, a Frame, a layout and some UICorners — that is the entire model. Here is the order Roblox builds a GUI in, with the code for each step.
StarterPlayer › StarterPlayerScripts › LocalScript
Roblox GUI code looks intimidating because a single panel can be forty lines of Instance.new calls. It is not complicated once you know the pattern: every GUI is a tree of objects under one ScreenGui, and Instance.new is just how you create each object in that tree. This page walks through the whole thing in the order Studio builds it.
Step 1: the ScreenGui is the container
Nothing appears on screen until a ScreenGui is parented to a player's PlayerGui. That object is the root of the interface: it decides which player sees it and whether it survives a respawn.
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "MyGameUI"
screenGui.ResetOnSpawn = false
screenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
screenGui.Parent = playerGui
ResetOnSpawn = false is the one line worth memorising. Leave it at the default and the GUI is destroyed every time the player dies — fine for a death screen, wrong for everything else.
Step 2: Frames are boxes, and they need a position and a size
A Frame is a rectangle. Its Position and Size are UDim2 values with two components: scale (a fraction of the parent) and offset (a fixed pixel count).
panel.AnchorPoint = Vector2.new(0.5, 0.5)
panel.Position = UDim2.fromScale(0.5, 0.5)
panel.Size = UDim2.fromScale(0.46, 0.6)
fromScale(0.5, 0.5) puts the panel in the middle of the screen at any resolution. AnchorPoint says which corner of the frame lands on that point — without it, (0.5, 0.5) puts the panel's top-left corner at the centre and it looks off-centre.
The safe rule: scale for anything that should stretch, offset for anything that should not (button height, icon size, padding).
Step 3: corners and borders are separate objects
Roblox does not put rounded corners on a frame. You add a UICorner as a child, and a UIStroke for a border:
local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(0, 8)
corner.Parent = panel
local stroke = Instance.new("UIStroke")
stroke.Thickness = 1
stroke.Transparency = 0.72
stroke.Color = Color3.fromRGB(230, 233, 239)
stroke.Parent = panel
This is why hand-written GUI code is long: a rounded bordered panel is three objects, not one. It also means you can change the corner radius later without touching the frame itself.
Step 4: layouts replace arithmetic
The alternative to placing every child by hand is a layout object. Three do most of the work:
UIListLayout— stacks children in one direction (HorizontalorVertical) with a padding between them.UIGridLayout— puts children in a grid with a fixed cell size and padding, which is what item slots and inventory tiles use.UIPadding— adds space inside the parent, so children never touch the edge.
local grid = Instance.new("UIGridLayout")
grid.CellSize = UDim2.fromOffset(96, 118)
grid.CellPadding = UDim2.fromOffset(8, 8)
grid.SortOrder = Enum.SortOrder.LayoutOrder
grid.Parent = items
With a grid in place, adding a seventh item card is one extra Instance.new — you never recalculate coordinates. Directly setting Position on children and parenting a layout will fight each other: pick one per container.
Step 5: text and buttons
TextLabel shows text, TextButton is clickable. Both have Text, TextSize, Font, TextColor3 and an alignment property. A button handler is a connection:
local buy = Instance.new("TextButton")
buy.Text = "BUY"
buy.Parent = slot
buy.MouseButton1Click:Connect(function()
-- your purchase logic
end)
Two things to get right: set Parent last so the handler never fires on a half-built button, and use Enum.Font.GothamBold (or any of the Gotham family) rather than the default SourceSans, which looks dated next to modern Roblox UI.
Step 6: getting the code into your game
There are two ways in:
- Command Bar (fastest to preview). View → Command Bar in Studio, paste the script, press Enter. The panel appears in
PlayerGuiimmediately. It disappears when you stop the session — good for checking a layout, not for shipping. - LocalScript (for the real game). Put the same code in a
LocalScriptunderStarterPlayer > StarterPlayerScripts. It runs on the client every time the player joins, and because it is inStarterPlayerScriptsit is not reset on respawn.
Use a LocalScript for anything visual. GUI code belongs on the client; the server should only be involved when a click needs to change game state, and that goes through a RemoteEvent.
The mistakes that cost the most time
- Parenting before configuring. Set properties first,
Parentlast. - Hard-coded positions. They break the moment the screen aspect ratio changes.
- Two layouts on one container. They overwrite each other and the result looks random.
- Forgetting
ResetOnSpawn = false. The GUI vanishes on death and looks like a bug. - Editing a GUI without
ScreenGui. Frames parented directly toPlayerGuiwill not render at all.
Reading a generated script
If you did not write the code yourself — say you copied a panel from a generator — read it in this order: find the ScreenGui, find the Frame that is its main child, then look for layout objects inside that frame. Everything else is decoration: colours, corners, strokes, font sizes. Once you can spot those four levels, a hundred-line script reads in about a minute.
Open the GUI maker to see all of this assembled for a shop, inventory, HUD or menu panel, and use the templates to start from something that already works.