Draft preview · this lesson is still being technically validated. Browser experiments and reference material remain available; trusted completion is enabled after publication.

← Back to journey

Review updates and session ownership

v2 · Activity 1 of 0 · updateasync · session-ownership · stale-writes · 28 min Requires Roblox Studio

Chapter outline
Loading draft…
Mini-mission

UpdateAsync transforms the current value atomically. Returning nil cancels that update. Its callback may be called again and cannot yield, so it must not contain waits, remote calls or an external reward side effect.

An ownership token identifies the session permitted to write. A lease expires, so every save must check both token and expiry. A server that lost ownership must stop state-changing play and saving rather than keep overwriting.

Your task

Create OwnedUpdate.apply(current,sessionId,now,newCompleted). Return nil for absent/invalid version, wrong owner, or expired lease. Otherwise copy the new completed sequence into a new record, retaining owner/expiry. Test stale owners and expiry boundaries.

Interface: Complete the documented Studio hierarchy and self-check the behavior outside the browser.

InputExpected
Syntax in this activity

New syntax: updateasync, session-ownership, stale-writes

Earlier ideas used here
  • pcall
  • data-load
  • failure-policy
  • rate-limits
  • duplicate-rewards
  • idempotency
Self-check rubric
  • Wrong session is denied.
  • The exact expiry boundary is denied.
  • The valid owner preserves the old record and copies the new sequence.
  • Explain what acquisition/renewal/release/order handling this module does not implement.

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/OwnedUpdate (ModuleScript)
ServerScriptService/TestOwnership (Script, RunContext=Server)
  1. Create the module and controlled local test file.
  2. Do not connect the integration sketch to a production store. Review the linked official session-locking discussion before designing a real save system.
ServerScriptService/OwnedUpdate.luau

Main guided reference implementation; use the hierarchy’s stated Script or ModuleScript type.

local OwnedUpdate = {}
function OwnedUpdate.apply(current, sessionId, now, newCompleted)
    if type(current) ~= "table" or current.version ~= 1 then return nil end
    if current.owner ~= sessionId or type(current.expiresAt) ~= "number" or current.expiresAt <= now then return nil end
    return { version = 1, owner = current.owner, expiresAt = current.expiresAt, completed = table.clone(newCompleted) }
end
return OwnedUpdate
ServerScriptService/TestOwnership.server.luau

Controlled stale/expired/current session cases without live datastore writes.

local Update = require(script.Parent.OwnedUpdate)
local old = { version = 1, owner = "session-a", expiresAt = 100, completed = {"lamp"} }
assert(Update.apply(old, "session-b", 50, {"gate"}) == nil)
assert(Update.apply(old, "session-a", 100, {"gate"}) == nil)
local nextRecord = Update.apply(old, "session-a", 50, {"lamp","gate"})
assert(nextRecord and #nextRecord.completed == 2 and #old.completed == 1)
print("Ownership decision checks passed")
ReviewedSaveCall.server.luau

Shows the nonyielding trusted transform call. Store access, lease acquisition and retries remain external reviewed responsibilities.

-- Integration sketch for a separate reviewed test environment, not a complete save system.
local function updateOwnedRecord(store, key, sessionId, now, snapshot, OwnedUpdate)
    return pcall(function()
        return store:UpdateAsync(key, function(current)
            return OwnedUpdate.apply(current, sessionId, now, snapshot)
        end)
    end)
end
return updateOwnedRecord

Expected output

  • Server Output prints Ownership decision checks passed.
  • Wrong and expired owners receive nil; current valid owner produces a separate updated record.

Studio assessment checklist

Multiplayer tests

  1. Run a local server and at least one client; compare server and client context where the activity uses both.
Troubleshooting and production considerations

Returning nil cancels a write; do not interpret a successful pcall alone as a confirmed updated record.

Session identity needs a unique server-generated token; Studio JobId may be empty, so a real design must account for that explicitly.

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.

Your goal

Create OwnedUpdate.apply(current,sessionId,now,newCompleted). Return nil for absent/invalid version, wrong owner, or expired lease. Otherwise copy the new completed sequence into a new record, retaining owner/expiry. Test stale owners and expiry boundaries.

Reset starter

Replace this code with the starter? Download your draft to keep a copy.

Code

Studio source file
Luau keyword highlighting · Ctrl/⌘ + Enter: Run · Esc: leave editor

Edit and run these files in Roblox Studio. This browser has no Roblox services, physics, or replication.

Your output will appear here.

Ready