Roblox Lua GUI Code Snippets: Copy-Paste Reference (2026)
2026-07-21 · Roblox GUI Maker Team
Create a ScreenGui programmatically
The foundation of every GUI. Create the ScreenGui, set its properties, and parent it to PlayerGui. ResetOnSpawn = false keeps the GUI alive across respawns so it does not vanish when the player dies and respawns. IgnoreGuiInset = true pushes the GUI under the top bar instead of leaving a gap, which matters for full-screen menus. Always parent the ScreenGui to PlayerGui, never to workspace or StarterGui at runtime, or it will not render on the player's screen.
1local Players = game:GetService("Players")2local player = Players.LocalPlayer34local screenGui = Instance.new("ScreenGui")5screenGui.Name = "MyGui"6screenGui.ResetOnSpawn = false7screenGui.IgnoreGuiInset = true8screenGui.Parent = player:WaitForChild("PlayerGui")
Add a Frame and TextLabel
A centered panel with a title. Use Scale-based sizing and AnchorPoint for true centering so the panel sits in the middle of any screen. AnchorPoint moves the reference point from the top-left corner to the center of the element, which means Position (0.5, 0, 0.5, 0) places the panel's center at the screen's center instead of its top-left corner. Skip AnchorPoint and your centered panel will drift down and to the right on larger screens.
1local panel = Instance.new("Frame")2panel.Size = UDim2.new(0.4, 0, 0.3, 0)3panel.Position = UDim2.new(0.5, 0, 0.5, 0)4panel.AnchorPoint = Vector2.new(0.5, 0.5)5panel.BackgroundColor3 = Color3.fromRGB(30, 30, 40)6panel.Parent = screenGui78local title = Instance.new("TextLabel")9title.Size = UDim2.new(1, 0, 0.3, 0)10title.BackgroundTransparency = 111title.Text = "Hello Roblox"12title.TextColor3 = Color3.fromRGB(255, 255, 255)13title.TextScaled = true14title.Parent = panel
Clickable button with feedback
A TextButton that runs a function on click, with a quick press animation for tactile feedback. Connect MouseButton1Click to a function that fires a RemoteEvent to the server for any game action, never trusting the client to decide game state directly. A press animation that scales the button down 5 percent for a tenth of a second makes the click feel responsive, and it costs one extra tween, so it is worth adding to every button.
1local button = Instance.new("TextButton")2button.Size = UDim2.new(0.5, 0, 0.4, 0)3button.Position = UDim2.new(0.25, 0, 0.4, 0)4button.BackgroundColor3 = Color3.fromRGB(109, 93, 251)5button.Text = "Click Me"6button.TextScaled = true7button.Parent = panel89button.MouseButton1Click:Connect(function()10 print("Button clicked!")11 -- Fire a RemoteEvent to the server for game actions12end)
TweenService: smooth animation
Animate any GUI property smoothly. The most common pattern is sliding a panel in from off-screen or fading it out. TweenService:Create takes the element, a TweenInfo with a time and easing style, and a table of target property values, then :Play() runs the animation at 60 frames per second. Animate Size, Position, BackgroundTransparency, or BackgroundColor3 depending on the effect you want, and chain tweens with Completed for multi-step motion.
1local TweenService = game:GetService("TweenService")23panel.Position = UDim2.new(0.5, 0, 1.5, 0) -- start off-screen4local tween = TweenService:Create(5 panel,6 TweenInfo.new(0.4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out),7 { Position = UDim2.new(0.5, 0, 0.5, 0) }8)9tween:Play()
DataStore: save and load
Persist player data across sessions. Always wrap DataStore calls in pcall to handle failures gracefully, because Roblox throttles DataStore requests and a failed GetAsync or SetAsync should never crash your game. Load data on PlayerAdded and save on PlayerRemoving as a baseline, and use UpdateAsync instead of SetAsync when multiple servers might write the same key, since UpdateAsync reads the current value before writing and avoids clobbering concurrent updates.
1local DataStoreService = game:GetService("DataStoreService")2local Players = game:GetService("Players")3local store = DataStoreService:GetDataStore("PlayerData")45local function loadData(player)6 local success, data = pcall(function()7 return store:GetAsync(player.UserId)8 end)9 return (success and data) or { coins = 0 }10end1112local function saveData(player, data)13 pcall(function()14 store:SetAsync(player.UserId, data)15 end)16end1718Players.PlayerAdded:Connect(function(player)19 local data = loadData(player)20 print(player.Name, "loaded:", data.coins)21end)
UIGridLayout: auto-arranged grid
Arrange child elements in a grid automatically. Perfect for inventories, shops, and item lists, where the number of items changes at runtime. Set CellSize and CellPadding once, parent the UIGridLayout to a ScrollingFrame, and every Frame you add afterward slots into the grid without manual positioning. Use Scale in CellSize if you want cells to resize with the screen, or Offset for fixed-size slots that stay a set number of pixels.
1local grid = Instance.new("UIGridLayout")2grid.CellSize = UDim2.fromOffset(120, 140)3grid.CellPadding = UDim2.fromOffset(8, 8)4grid.FillDirection = Enum.FillDirection.Horizontal5grid.SortOrder = Enum.SortOrder.LayoutOrder6grid.Parent = itemContainer78-- Every Frame you parent to itemContainer now arranges in a grid automatically
UIListLayout: vertical stack
Stack elements vertically (or horizontally) with consistent spacing. The standard way to build menus and lists.
1local list = Instance.new("UIListLayout")2list.FillDirection = Enum.FillDirection.Vertical3list.Padding = UDim.new(0, 12)4list.HorizontalAlignment = Enum.HorizontalAlignment.Center5list.SortOrder = Enum.SortOrder.LayoutOrder6list.Parent = menuContainer