Draft preview · this lesson is still being technically validated. Browser experiments and reference material remain available; trusted completion is enabled after publication.
v2 · Activity 2 of 4 · pcall · data-load · failure-policy · 28 min Requires Roblox Studio
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.
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 |
|---|
local ok, result = pcall(read)
if not ok then return { ok = false, reason = "unavailable" } endA failed request is not evidence that a record is absent. Keep the operational outcome explicit.
New syntax: pcall, data-load, failure-policy
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.
ServerScriptService/ProfileLoader (ModuleScript) ServerScriptService/TestLoading (Script, RunContext=Server)
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 ProfileLoaderLocal 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")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.
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.
The lab returns ok=false and no profile on failure or unknown schema. It does not activate progression or permit saving fallback data after a failed load. Do not expose internal error strings to players.
Replace this code with the starter? Download your draft to keep a copy.
Edit 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.