Lesson 2 of 5
ProfileStore: the library pros use
Replace a hand-rolled Roblox DataStore layer with ProfileStore, covering install with Wally, sessions, session locking, autosave, the Mock store and a one-time migration of Beacon Yard's saved coins.
By the end you can
- Install ProfileStore with Wally and start, use and end a session with its API
- Explain how ProfileStore's session locking, autosave and steal timing differ from the hand-rolled 45-second lock
- Migrate Beacon Yard's saved coins from the old record to a ProfileStore profile exactly once
Before this: DataStores: saving without losing a single coin
1Everything DataService does by hand
Open src/server/DataService.luau at m09-react-shop. It is about 389 lines. It loads with UpdateAsync, writes its own sessionJob and sessionAt lock, tries a failed save up to four times (1 initial try and 3 retries), saves everyone in BindToClose, and guards against saving over a failed load. It has no autosave, so a long session's lock looks expired after 45 seconds. You saw that gap in the last lesson.
The hand-rolled version saving and loading. After the migration, a player must see the same coins.
Transcript
- 0:00 Coins increase in memory when you pick one up.
- 0:05 Stop ends the session and BindToClose saves if the key is savable.
- 0:10 The next Play loads the same coins.
- 0:20 GetAsync prints the row. The game writes it with UpdateAsync.
local LOCK_SECONDS = 45
Which fields of the hand-rolled record exist only to implement the session lock?
Answer
sessionJob; sessionAt. sessionJob and sessionAt are the sign-out sheet. coins and speedOwned are the player's data, the only part worth carrying into the new store.
Open this step in lesson mode · Link to this step · This confused me
2ProfileStore
ProfileStoreProfileStoreloleris's free module that loads a player's data once, locks it to one server and autosaves it.Think of it as a librarian who checks a file out to one desk at a time and files it back for you. is a free, open-source module by loleris (MAD STUDIO) that loads a player's data once, locks it to one server, autosaves it, and saves it a final time when you end the session1. It is a single ModuleScript under the Apache 2.0 licence, and its DevForum thread is active2. The data you work with is a plain table, profile.Data.
ProfileStore replaces the same author's ProfileService, whose README now says it is no longer supported and points new projects to ProfileStore3. If a tutorial says ProfileService, it is out of date.
Its README is also honest about what it is not for: it is built for one profile per player and is "not designed (and never will be) for in-game leaderboards or any kind of global state". Leaderboards get their own lesson.
Which of these jobs suit ProfileStore?
Answer
A player's coins and upgrades; A player's settings, such as music muted. Per-player data is exactly what it is for. A leaderboard needs an OrderedDataStore, and a shared queue needs MemoryStore; both are read and written by many servers at once.
Open this step in lesson mode · Link to this step · This confused me
3Install it as a server package
ProfileStore is on Wally as lm-loleris/profilestore, and version 1.0.3 was the newest on 2026-11-231. Its realm is server, so it goes under [server-dependencies], and wally install puts it in a ServerPackages folder. Map that folder into ServerScriptService, where clients cannot see it.
[server-dependencies]
ProfileStore = "lm-loleris/profilestore@1.0.3"
"ServerScriptService": {
"Server": { "$path": "src/server" },
"ServerPackages": { "$path": "ServerPackages" }
}
Go deeper What a realm is
Wally sorts packages into realms that say where they are meant to run: shared, server or dev. ProfileStore's own wally.toml declares realm = "server", so it installs under [server-dependencies] into a ServerPackages folder rather than Packages.
Why does ProfileStore live in ServerScriptService rather than ReplicatedStorage?
Answer
It only runs on the server, and clients have no reason to see data-layer code. DataStores are server-only. Keeping server packages in ServerScriptService also means exploiters cannot read the code.
Open this step in lesson mode · Link to this step · This confused me
4Sessions: start, use, end
The whole API for a player is a handful of calls4:
Reference ProfileStore, the parts Beacon Yard uses
Async methods yield until they have a result; the rest return at once.
ProfileStore.New(storeName, template) -> store- Like GetDataStore. New profiles start as a deep copy of template.
store:StartSessionAsync(key, { Cancel = fn }?) -> Profile?- Loads the profile and locks it to this server. Waits if another server holds it. Returns nil in rare conflicts.
profile.Data- The plain table you read and change. Saved on the next autosave and on EndSession.
profile:Reconcile()- Fills in fields that exist in the template but not in Data.
profile:AddUserId(userId)- Tags the key with the player's id for data-deletion requests.
profile.OnSessionEnd:Connect(fn)- Fires when the session ends here, including when another server takes it.
profile:EndSession()- Final save and release. Always call it when the player leaves.
store.Mock- The same API on an in-memory store that is never saved.
local store = ProfileStore.New("BeaconYard_v2", { coins = 0, speedOwned = false })
local profile = store:StartSessionAsync("Player_" .. player.UserId, {
Cancel = function()
return player.Parent ~= Players
end,
})
if profile == nil then
return -- no lock: the docs suggest player:Kick() here; Beacon Yard keeps the visit in memory and never saves it
end
profile:AddUserId(player.UserId)
profile:Reconcile()
profile.OnSessionEnd:Connect(function()
if player.Parent == Players then
player:Kick("Your profile was opened on another server.")
end
end)
local session = playerStore:StartSessionAsync(profileKey(player.UserId), {
Cancel = function()
return player.Parent ~= Players
end,
})
That is the real call in Beacon Yard at m10-memory.
The nil case is rare, and the ProfileStore docs say to kick the player when it happens. Beacon Yard chooses differently: the player keeps playing on a fresh copy held only in memory, and nothing from that visit is ever saved. The cost is real. Everything the player earns in that visit is gone when they leave. In return, a rare hiccup does not end their session. A game that charges Robux should kick instead, so nothing a player paid for can vanish.
What is the Cancel function for?
Answer
To stop waiting for the lock if the player leaves before the session starts. ProfileStore calls Cancel while it waits. Returning true tells it the player is gone, so it stops making DataStore requests for nobody.
Open this step in lesson mode · Link to this step · This confused me
5How ProfileStore holds the lock
ProfileStore keeps the lock inside its own record and renews it with an autosave every 300 seconds. When a second server asks for a key that is still held, ProfileStore asks the holder to save and let go, using MessagingService, then re-reads every few seconds. If the holder does not answer within about 40 seconds, the new server steals the session. A profile with no update for 630 seconds is assumed to belong to a crashed server5.
Stored dataRemember: Session lock is the vault sign-out sheet: Server A has this account, signed at 14:02.
Compare that with DataService: a 45-second window that nothing renews, and no way to ask the holder to hurry. ProfileStore's sheet is signed again on every autosave, and the waiting server can knock on the door.
Where does it break?“The vault sign-out sheet: Server A has this account, signed at 14:02” is a good picture of session lock. Where does it stop being true?
It breaks here. Nobody enforces the sheet; it only works because every server that touches the key checks it, and an unrenewed lock looks expired.
The true model. A session lock records which server holds a player's data so two servers do not write it at once. Beacon Yard's hand-rolled lock expires after 45 seconds so a crashed server cannot hold a key forever. ProfileStore renews its lock with an autosave every 300 seconds.
Even ProfileStore's sheet is only a convention. Nothing in the DataStore stops a server from ignoring it, and ProfileStore itself has an option that does exactly that: StartSessionAsync(key, { Steal = true }) takes the session without waiting for the holder's final save.
A teammate loads player data with StartSessionAsync(key, { Steal = true }) to make joins faster. What is wrong?
Answer
Steal skips the lock, so a second server can load stale data and duplicate items. Steal bypasses the session lock that prevents dupes. The docs say to use it only for debugging, never for loading player data.
Open this step in lesson mode · Link to this step · This confused me
6Moving the old coins across, once
You cannot point ProfileStore at BeaconYard_v1. It stores its own structure (the data plus metadata such as the lock), and it treats any other table at a key as overwritten data, which it would replace. So the m10-memory milestone moves profiles to a new store, BeaconYard_v2, with keys like Player_123, and copies each player's old record across the first time they load.
The copy is a small pure module, DataMigration. A flag in the profile, legacyImported, makes sure it happens once. The rule: coins add up, speed is kept if either side has it.
local function apply(data, raw)
if data.legacyImported then return false end
data.legacyImported = true
if typeof(raw) ~= "table" then return false end
local coins = raw.coins
if typeof(coins) == "number" and coins == coins and coins >= 0 and coins < math.huge then
data.coins += math.floor(coins)
end
data.speedOwned = data.speedOwned or raw.speedOwned == true
return true
end
local data = { coins = 0, speedOwned = false, legacyImported = false }
local old = { coins = 120, speedOwned = true, sessionJob = "abc", sessionAt = 1794542891 }
print(tostring(apply(data, old)) .. " " .. data.coins .. " " .. tostring(data.speedOwned))
print(tostring(apply(data, old)) .. " " .. data.coins)
true 120 true
false 120Why does the second apply return false and leave 120 coins?
Answer
legacyImported is already true, so the old record is never added twice. The flag lives in the profile and is saved with it. Without it, every login would add the old 120 coins again: a duplication bug.
Open this step in lesson mode · Link to this step · This confused me
7What studios actually use
Retiring the analogy
You no longer need “the vault sign-out sheet: server a has this account, signed at 14:02”. A session lock records which server holds a player's data so two servers do not write it at once. Beacon Yard's hand-rolled lock expires after 45 seconds so a crashed server cannot hold a key forever. ProfileStore renews its lock with an autosave every 300 seconds.
Other ways pros do this
| Option | What it is | When pros pick it | Trade-offs |
|---|---|---|---|
| ProfileStore | Session-locked profiles with autosave, steal timing, Mock and a one-file install. | The current default for per-player data in most new Roblox games. | Its record format is its own, and it does not do leaderboards or shared state. |
| Hand-rolled | Your own UpdateAsync, lock, autosave and retries, like Beacon Yard's DataService. | Learning, or data that no library fits. | Every edge case is yours to find, usually in production. |
| Suphi's DataStore Module | Another session-locking module, popular on the DevForum since 2023. | Teams that prefer its API and its own documentation. | A different author and conventions; compare the two before you commit. |
| ProfileService (legacy) | ProfileStore's predecessor by the same author. | Only in games that already use it. | No longer supported; its README sends new projects to ProfileStore. |
| DataStore2 (legacy) | An older caching wrapper. | Maintaining old games. | Last updated in 2023, no session lock. |
You turned on Studio access to API services to test purchases. How do you stop Studio sessions from writing to real players' profiles?
Answer
Use store.Mock when RunService:IsStudio() is true. The ProfileStore docs show swapping in store.Mock in Studio. It never touches the DataStore, so tests cannot damage real data.
Open this step in lesson mode · Link to this step · This confused me
8Build along: the m10 migration
The m10-memory milestone is tagged, so you can compare your work with git diff m09-react-shop m10-memory.
Build along
wally.tomldefault.project.jsonsrc/shared/Config.luausrc/server/DataMigration.luausrc/server/DataService.luau
Add ProfileStore under
[server-dependencies]inwally.tomland runwally install.You should see ServerPackages/ProfileStore.lua exists and wally.lock lists lm-loleris/profilestore 1.0.3.
Map
ServerPackagesintoServerScriptServiceindefault.project.json, and addPROFILE_STORE_NAME = "BeaconYard_v2"toConfig.You should see ServerScriptService.ServerPackages.ProfileStore appears in Explorer after Rojo syncs.
Create
src/server/DataMigration.luauwith the template (coins,speedOwned,legacyImported) and theapplyfunction from this lesson.You should see DataMigration.luau has template, readLegacy and apply, and requires nothing.
Rewrite
DataServicearoundStartSessionAsync,Reconcile,OnSessionEndandEndSession, call the migration once per session, and keep its public functions (addCoins,trySpend,save) the same so the rest of the game does not change.You should see On Play, a player with an old BeaconYard_v1 record sees the same coins, and the next Play does not add them again.
Open this step in lesson mode · Link to this step · This confused me
9Challenges
Level 2, Builder
Challenge
Make Studio sessions use ProfileStore's Mock so testing never writes real keys.
- Start from
- The game at milestone m10 (download)
Done means
- In Studio, DataService uses the Mock store
- In a live server it uses the real store
- Output says which one is in use, once
Hints
- RunService:IsStudio()
- Swap the store before any session starts.
- Print once at start, not per player.
Level 3, Professional
Challenge
Add a saved field the ProfileStore way. Compare how many places you would have had to edit in the hand-rolled DataService. The test is challenges/profilestore.luau, and the solution is challenges/solutions/profilestore.md.
- Start from
- The game at milestone m10 (download)
- Grade it
- To grade work from an earlier milestone, apply your challenge changes to a checkout based on
m14-ship, which includes the Course Runner. In Studio, press Play, then rungame.ServerScriptService.RunGameTests:Invoke("profilestore", "profilestore-new-field")in the server command bar. Copy the pass code from Output.
Done means
- The template holds dronesDowned = 0
- Reconcile adds dronesDowned to a profile saved before the field existed
- challenges/profilestore.luau prints TEST PASS for every check
Hints
- Add the field to the type and to DataMigration.template.
- Reconcile is already called after StartSessionAsync.
- The test uses the Mock store, so it runs in an unpublished place.
A player leaves server A and joins server B two seconds later. A is healthy and ending the session. What normally happens on B?
Answer
B waits while A finishes its final save and releases, then loads the saved data. A's EndSession saves and releases; B's re-read sees the released lock and loads the new data. Stealing after about 40 seconds and the 630-second dead check are for servers that never answer.
Open this step in lesson mode · Link to this step · This confused me
10Recap
Recap
- ProfileStore (lm-loleris/profilestore, a server package) is the current choice; ProfileService and DataStore2 are legacy.
- StartSessionAsync locks a profile to one server; autosave every 300 s renews it, a waiting server asks the holder to release and steals after about 40 s.
- ProfileStore needs its own store, so old records are copied once into BeaconYard_v2, guarded by a legacyImported flag.
Session lock is the vault sign-out sheet: Server A has this account, signed at 14:02, until nobody enforces the sheet; it only works because every server that touches the key checks it, and an unrenewed lock looks expired.
Queued for review
Open this step in lesson mode · Link to this step · This confused me