Session locking in Roblox: what ProfileService-style modules do and why
Short answer
When a player hops servers quickly, server B can load their data before server A has finished saving, then A's late save overwrites B's progress (or the reverse). A session lock stores which server owns the profile and when it last saved; a server only loads a profile that is unlocked or stale, and only saves while it holds the lock. ProfileService and similar modules implement this inside UpdateAsync.
Copy-paste code
The core of a session lock (inside UpdateAsync)
local SESSION = game.JobId ~= "" and game.JobId or "studio"
local STALE = 30 * 60 -- a lock older than 30 min is treated as abandoned
-- returns the profile if this server now owns it, or nil if another live server does
local function acquire(store, key)
local owned
local ok = pcall(store.UpdateAsync, store, key, function(old)
old = old or { data = { coins = 0 } }
local lock = old.lock
if lock and lock.session ~= SESSION and os.time() - lock.at < STALE then
return nil -- someone else holds it: change nothing
end
old.lock = { session = SESSION, at = os.time() }
owned = old
return old
end)
return ok and owned or nil
end
local function saveAndRelease(store, key, data, release)
pcall(store.UpdateAsync, store, key, function(old)
if not old or not old.lock or old.lock.session ~= SESSION then return nil end -- lost the lock: don't write
old.data = data
old.lock = (not release) and { session = SESSION, at = os.time() } or nil
return old
end)
endGood to know
If acquire returns nil, wait a few seconds and retry; the other server usually releases the lock when it finishes its PlayerRemoving save.
Autosaves refresh lock.at, so a server that crashes leaves a lock that goes stale after 30 minutes.
Studwright: Roblox systems that never lose saves
from $12Studwright's SaveData is a tested session-locked save module (one server owns a save at a time), with 15 more systems in the kit.
Secure checkout · 14-day fix-or-refund if it doesn't work as described (terms)Questions
Do small games need this?
Most small games are fine without it. Add it when items cost real Robux or players hop servers often.
Is ProfileService still maintained?
Its author has released ProfileStore as a successor; both use this session-lock idea.
Free: the Roblox Starter Kit
7 free Luau modules (damage, pets, shop formatting, admin audit log, tycoon guards and more) plus 3 free calculators, then 5 short emails with the scripting fixes we see go wrong most often.