08 UI and client scripts
Studio + Cursor workflow for this lesson
Observable outcome: Each client renders its own HUD from replicated server-owned Coins; respawning does not duplicate the HUD.
App roles: Build and inspect non-script instances, run simulations, read server/client Output, use the debugger, and verify behavior in Roblox Studio. Read and edit synchronized Luau, attach narrow file context, and review the saved diff in Cursor. Cursor writes edits to disk immediately; Script Sync may apply them to Studio before you finish reviewing. Stop the simulation first and never treat Cursor's explanation as a gameplay test.
Starting hierarchy and synced files
Create or confirm this in Studio. Scene Parts remain Studio-owned; only the named script folders are synced.
StarterPlayer
StarterPlayerScripts
CourseClient Folder, synced to src/client
CoinHud LocalScript
Players
<Player>
PlayerScripts
CourseClient
CoinHud runtime copy
Disk file(s): src/client/08-coin-hud.local.luau. The suffix is part of the contract: .server.luau creates a server Script, .local.luau creates a LocalScript, and plain .luau creates a ModuleScript. This course does not use .client.luau when it specifically needs a LocalScript.
Predict before asking
Predict which values the client may display and which values it must never own. Explain why .local.luau is used instead of .client.luau in this course.
Copy this narrow Cursor prompt
Review @src/client/08-coin-hud.local.luau. Do not edit. Explain the LocalScript lifecycle, WaitForChild timeouts, event-driven rendering, ResetOnSpawn, and why this file must never assign Coins.Value. Compare `.local.luau` with Script Sync's `.client.luau` RunContext behavior.
Do not include credentials, cookies, private chats, account details, personal names, or production player data. Attach only the listed synchronized files; Cursor cannot see the full Studio scene merely because it can see scripts.
Review the saved diff
Check that one runtime copy creates one ScreenGui, the initial render is called, and the view cannot award itself score. Keep the change small enough to explain line by line. If Cursor edits more than requested, stop, revert to your checkpoint, and narrow the prompt. Do not wait for a fictional “accept before sync” step: saved edits may already be visible in Studio.
Test in Studio
Play, collect a coin, and reset the character. Inspect client Explorer and client Output: one HUD must remain and show the server score.
Negative case: Disable or rename the server Collectibles script and confirm the client fails with a diagnostic instead of inventing a local score.
Copy this debugging prompt
Using @src/client/08-coin-hud.local.luau, diagnose: expected one HUD with server Coins; observed [result]; client Output [exact lines]; relevant Explorer branch [paste]. Suggest one minimal edit and a respawn retest.
Cursor can reason over the supplied evidence, but it cannot operate Studio's debugger or certify that the retest passed.
Learn it yourself
Add a display-only progress label yourself. Ask Cursor to check accessibility, duplicate UI risk, and that no client code changes authoritative state.
Checkpoint
After you observe the expected and negative results, Stop the simulation, review the complete diff, and make a small version-control commit or copy a known-good local checkpoint. Record what you personally verified; do not convert an unchecked Studio test into a claim.
Before you start and what you will build
Continue the obby/collectibles place from Collectibles and score. Keep its server scripts enabled. Your player must already have a leaderstats Folder containing an IntValue named Coins during Play. This lesson adds a small heads-up display, or HUD. Collecting a coin updates both the leaderboard and the HUD; the HUD survives character death.
For an experienced programmer, think of this as a view bound to replicated state. The view does not own the score. That separation becomes important when clients can request gameplay actions in Remotes and validation.
Where the code runs
Create src/client/08-coin-hud.local.luau in Cursor; Script Sync creates a LocalScript, not a regular Script, under StarterPlayer > StarterPlayerScripts > CourseClient. Studio copies this template into the joining player's PlayerScripts. Each client runs its own copy. Players.LocalPlayer identifies the player on that client. Do not place this LocalScript in ReplicatedStorage or ServerScriptService. Roblox documents these execution locations in Script types and locations.
The script creates its UI at runtime. You do not also create a ScreenGui in StarterGui for this example. Before Play, the only added object is:
StarterPlayer
StarterPlayerScripts
CourseClient synchronized Folder
08-coin-hud LocalScript
During Play, look in the client Explorer for:
Players
YourPlayer
leaderstats
Coins IntValue, created by the server
PlayerGui
CoinHud ScreenGui, created by this client
CoinsLabel TextLabel
PlayerScripts
CourseClient
08-coin-hud runtime copy of the LocalScript
Complete implementation
Replace the new LocalScript's default contents with this entire file. This is additive: it does not replace Collectibles or any checkpoint/hazard script.
Download 08-coin-hud.local.luau
-- LocalScript: StarterPlayer > StarterPlayerScripts > CourseClient > CoinHud
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local gui = Instance.new("ScreenGui")
gui.Name = "CoinHud"
gui.ResetOnSpawn = false
gui.Parent = playerGui
local label = Instance.new("TextLabel")
label.Name = "CoinsLabel"
label.Position = UDim2.fromOffset(16, 16)
label.Size = UDim2.fromOffset(220, 48)
label.BackgroundColor3 = Color3.fromRGB(25, 30, 40)
label.TextColor3 = Color3.fromRGB(255, 235, 120)
label.Font = Enum.Font.GothamBold
label.TextSize = 24
label.Text = "Coins: loading..."
label.Parent = gui
local leaderstats = player:WaitForChild("leaderstats", 10)
assert(leaderstats, "CoinHud needs lesson 07's Collectibles server script")
local coins = leaderstats:WaitForChild("Coins", 10)
assert(coins and coins:IsA("IntValue"), "Expected leaderstats.Coins IntValue")
local function render()
label.Text = string.format("Coins: %d", coins.Value)
end
coins:GetPropertyChangedSignal("Value"):Connect(render)
render()
Understand the useful details
The ScreenGui is the root of this on-screen interface. ResetOnSpawn = false keeps it through ordinary character respawns. The script belongs in StarterPlayerScripts so it runs once per player session rather than making another HUD each time the character spawns. UI objects rendered on a player's screen live in PlayerGui; ScreenGui documents the container and its screen-layout controls.
UDim2.fromOffset(220, 48) gives the label a fixed width and height. Position places its top-left corner 16 pixels from the available UI area's top-left. Fixed offsets are easy to inspect while learning; later, Cross-device controls examines smaller screens and safe placement. The label is deliberately large enough to read at a glance. Avoid conveying success only with color, since some players cannot distinguish those colors.
The server and client do not initialize every object simultaneously. WaitForChild avoids reading leaderstats before it arrives. Here a ten-second timeout produces an actionable assertion if the prerequisite is missing. It is a diagnostic limit, not a network guarantee. If a deliberately slow loading design needs longer, change the timeout intentionally.
GetPropertyChangedSignal("Value") subscribes to future changes. Calling render() immediately afterward also handles the current count. Without that initial render, a player who already has coins might see “loading” until the next pickup. There is no frame loop and no polling timer: the interface changes only when its input changes.
The script never assigns coins.Value. Treat that as a code-review invariant. If you later animate the number, keep the real count separate from an interpolated display count. An attractive animation must not become the game's accounting system.
Verify it yourself
- Use Play, not a server-only Run test. The label should read
Coins: 0after startup. - Touch an uncollected coin. The label and leaderboard should both increase by one.
- Touch the same coin again. The lesson 07 per-player pickup rule should prevent another award.
- Reset your character. There should still be exactly one HUD, showing the same session count.
- Stop and Play again. Without persistence, the new session starts at zero.
The examples have been statically reviewed, not run in Roblox Studio for this manual. These checks are the acceptance test, not a claim that execution was already verified.
Troubleshooting
- Nothing appears: confirm the object's class is LocalScript, its name is
CoinHud, and its parent is StarterPlayerScripts.CourseClient. Check client Output for an earlier error. - Loading remains: inspect
Players > YourPlayer > leaderstatsduring Play. Exact capitalization matters. Fix the prerequisite server script instead of creating a second client-owned score. - Two labels overlap: remove the duplicate template/script; do not move one label to disguise a duplicate execution path.
- Edits disappear after Stop: you edited the runtime copy. Stop first, then edit the StarterPlayerScripts source.
Exercises
- Add a second TextLabel explaining the three-coin boost requirement. Keep it display-only.
- Change the label's background and text while maintaining readable contrast.
- Predict what happens if
render()is removed, then test your prediction and restore it.
Sources and next steps
API and placement references last checked 2026-10-03: script locations, PlayerScripts, ScreenGui.
Previous: 07 Collectibles and score · Next: 09 Client-server boundary
Related: 04 Events and debugging · 13 Cross-device controls · Glossary