Draft preview · this lesson is still being technically validated. Browser experiments and reference material remain available; trusted completion is enabled after publication.
Handle loading failure without overwriting progress
v2 · Activity 1 of 0 · pcall · data-load · failure-policy · 28 min Requires Roblox Studio
Chapter outline
pcall runs a function and reports success plus its result or error. A successful nil result can mean no record; a failed call is an operational problem. These outcomes need different policies.
The Loader below accepts a read function so controlled tests can simulate failure without calling live storage. In a real server, the trusted read function can call GetAsync on a dedicated test store.
Your task
Implement ProfileLoader.load(read). Successful nil -> default version-1 profile. Valid version-1 table -> copied profile. Failure or wrong schema -> ok=false with reason; do not return a saveable default for those cases.
Interface: Complete the documented Studio hierarchy and self-check the behavior outside the browser.
| Input | Expected |
|---|
Syntax in this activity
New syntax: pcall, data-load, failure-policy
Earlier ideas used here
- persistent-state
- temporary-state
- schema
- nil
- guards
Self-check rubric
- Absent, failed, valid and invalid-schema outcomes all behave differently as specified.
- A failure is presented as service unavailability rather than a learner or player mistake.
- No fallback profile from a failed read is written anywhere.
Your explanation is a self-check. Exact wording is not automatically graded.
Self-checked Studio exercise. A saved checklist records your own review; no uploaded place file or screenshot is executed here.
Build this hierarchy
ServerScriptService/ProfileLoader (ModuleScript) ServerScriptService/TestLoading (Script, RunContext=Server)
- Place the solution module in ProfileLoader and the controlled test file beside it.
- Keep real API access disabled for these tests. If exploring GetAsync later, use a separate published test experience and a separate test key, never the live game’s store.
ServerScriptService/ProfileLoader.luau
Main guided reference implementation; use the hierarchy’s stated Script or ModuleScript type.
local ProfileLoader = {}
function ProfileLoader.load(read)
local ok, data = pcall(read)
if not ok then return { ok = false, reason = "unavailable" } end
if data == nil then return { ok = true, profile = { version = 1, completed = {}, soundEnabled = false } } end
if type(data) ~= "table" or data.version ~= 1 or type(data.completed) ~= "table" or type(data.soundEnabled) ~= "boolean" then
return { ok = false, reason = "invalid-schema" }
end
local completed = {}
for _, id in ipairs(data.completed) do
if type(id) ~= "string" then return { ok = false, reason = "invalid-schema" } end
table.insert(completed, id)
end
return { ok = true, profile = { version = 1, completed = completed, soundEnabled = data.soundEnabled } }
end
return ProfileLoaderServerScriptService/TestLoading.server.luau
Local controlled read fixtures; no production datastore access.
local Loader = require(script.Parent.ProfileLoader)
local absent = Loader.load(function() return nil end)
assert(absent.ok and absent.profile.version == 1)
local failed = Loader.load(function() error("controlled read failure") end)
assert(not failed.ok and failed.profile == nil)
local valid = Loader.load(function() return { version = 1, completed = {"lamp"}, soundEnabled = false } end)
assert(valid.ok and valid.profile.completed[1] == "lamp")
local invalid = Loader.load(function() return { version = 99 } end)
assert(not invalid.ok and invalid.profile == nil)
print("Loading policies checked")Expected output
- Server Output prints Loading policies checked.
- Failure and invalid schema have no profile field that could accidentally be saved.
Studio assessment checklist
Multiplayer tests
- Run a local server and at least one client; compare server and client context where the activity uses both.
Troubleshooting and production considerations
pcall returning false is distinct from pcall returning true,nil.
Validate persistent data even if an earlier version of your code wrote it.
This is a bounded learning lab. It has not been executed in an approved Roblox Studio environment by this website.
A checked checklist records your own review, not automated project competence or a community verification.
Implement ProfileLoader.load(read). Successful nil -> default version-1 profile. Valid version-1 table -> copied profile. Failure or wrong schema -> ok=false with reason; do not return a saveable default for those cases.
Reset starter
Replace this code with the starter? Download your draft to keep a copy.
Code
Studio source fileEdit and run these files in Roblox Studio. This browser has no Roblox services, physics, or replication.
Your output will appear here.
Sign in with RoCreators to save private progress across devices. Community verification is separate.