Module 12 · Modules, persistence, and the capstone
Data persistence without data loss
Use server-only DataStore calls, pcall, UpdateAsync, defaults, and a schema version without pretending this is a complete production system.
What you will learn
- Keep DataStore access server-only
- Wrap service calls with pcall
- Use UpdateAsync and versioned defaults carefully
- DataStore
- A Roblox service for persistent server-side key-value data.
- schema version
- A number identifying the shape expected for stored data.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Players expect Coin Quest progress to survive sessions, but careless saves can overwrite newer data or turn temporary service failures into data loss.
The mental model
Only the server accesses DataStoreService. Calls can fail, so pcall contains the failure and the game needs an honest recovery path.
UpdateAsync receives the latest stored value and returns the intended next value. Defaults and schemaVersion make missing or older shapes explicit.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
local is read as “local”: it creates a name that is available only in the current code scope.
== is read as “equals equals”: it compares two values and produces a boolean.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
else is read as “else”: it provides the fallback branch.
end is read as “end”: it closes a block such as a decision, loop, or function.
A tiny example
--!strictlocal DataStoreService = game:GetService("DataStoreService")local store = DataStoreService:GetDataStore("CoinQuestProfileV1")type Profile = { schemaVersion: number, coins: number }local DEFAULT: Profile = table.freeze({ schemaVersion = 1, coins = 0 })local function saveCoins(userId: number, coins: number): boolean local key = `player:{userId}` local ok = pcall(function() store:UpdateAsync(key, function(previous) local oldCoins = if typeof(previous) == "table" and typeof(previous.coins) == "number" then previous.coins else DEFAULT.coins return { schemaVersion = 1, coins = math.max(oldCoins, coins) } end) end) return okendreturn saveCoinsLine by line
- 3
DataStoreService is requested only in server code.
- 4
The store name includes an intentional version boundary.
- 6
The stored shape has a schema version and coin count.
- 7
An immutable default handles absent data.
- 9
The helper returns whether saving succeeded.
- 11
pcall contains service errors.
- 12
UpdateAsync receives the latest value for an atomic transform.
- 13
Runtime checks validate the old shape before reading coins.
- 14
The returned table is the next stored value.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Start with the statements outside function bodies. A function definition creates a callable value; its body runs when a call, event, or scheduled task reaches it.
Trace the arguments at each call and the branch or loop inside that call. Return to the caller when the function returns. For an event, use the action named in Try it to trigger the callback.
Line 3: DataStoreService is requested only in server code.
Line 4: The store name includes an intentional version boundary.
Line 6: The stored shape has a schema version and coin count.
Line 7: An immutable default handles absent data.
Line 9: The helper returns whether saving succeeded.
Line 11: pcall contains service errors.
Line 12: UpdateAsync receives the latest value for an atomic transform.
Line 13: Runtime checks validate the old shape before reading coins.
Line 14: The returned table is the next stored value.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: Every prior-value case produces a versioned shape, and failures remain visible to server code.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
What happens when previous data has no numeric coins field?
Try it
Mark each step after you have tried it.
Common mistake
This compact transform is not a complete production persistence system. It omits session locking, retry policy, shutdown budgeting, migrations, and conflict design; do not present five lines as data-loss-proof saving.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Persistence is server-only and fallible.
- UpdateAsync transforms the latest value.
- Defaults and schema versions make data shape explicit.
Next: Next, you will diagnose bugs and measure real bottlenecks before optimizing.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.