Guide · 9 min read · Updated November 2026
Roblox DataStore script with session locking: what AI should write, and how to check it
Most "DataStore script" tutorials and most AI-generated saving code share one bug: two servers can hold the same player's data at once, both save, and the player ends up with duplicated pets or a rolled-back purchase. The fix is a session lock. This guide shows what a correct saving module looks like, the prompt that gets an AI agent to write one inside your Studio, and the five things to check before you trust it.
The bug in most saving scripts
The usual script loads with GetAsync on join, keeps a table in memory, and writes it with SetAsync on leave and every minute. It passes every Studio test and fails in production, because production has more than one server. A rejoin after a crash, a teleport between places, or a slow PlayerRemoving on a shutting-down server means two servers hold the same key. Both save. Items duplicate, purchases roll back, and the Discord fills with "I lost my pet".
A session lock fixes it: loading writes "this server owns this key" into the data itself, a second server that sees a fresh lock waits or gives up, every save checks the lock still belongs to this server, and leaving releases it. You can hand-roll that with UpdateAsync, or use a module that already does it.
What correct looks like: ProfileStore
Forge's default, from its datastores playbook, is ProfileStore (by loleris): session locking, autosave, BindToClose and reconcile built in. The shape of the module Forge writes:
- One module owns the data.
ServerScriptService.Modules.PlayerDatais the only script that touches DataStoreService or the profile. Shops, remotes and ProcessReceipt callPlayerData.GetandPlayerData.WaitFor; nothing else reads a store. - A template with everything you will ever save, including
PurchaseIdCache = {}so developer products can be idempotent later. New fields are added byReconcile, so old saves pick them up. - A session per player.
StartSessionAsync("Player_" .. userId, { Cancel = function() return player.Parent ~= Players end }),AddUserIdfor GDPR, kick with a clear message when the session fails,EndSessionon leave. - Mock in Studio.
Store = Store.MockunderRunService:IsStudio(), so playtests never write the live keys. - Plain data only. Tables, numbers, strings, booleans. No Instances, no Vector3 or Color3, no arrays with gaps.
Studio needs Game Settings ▸ Security ▸ Enable Studio Access to API Services turned on, or ProfileStore prints that API services are unavailable and nothing persists.
The prompt
Add saving to my game with ProfileStore (insert the Creator Store module by loleris if it is
missing). One module, ServerScriptService.Modules.PlayerData, owns every read and write: a
TEMPLATE with Coins, Unlocks, Equipped and PurchaseIdCache, StartSessionAsync with a Cancel
when the player leaves, Reconcile, AddUserId, kick on a failed session, EndSession on
PlayerRemoving, and a WaitFor the remotes call. Use Store.Mock in Studio so tests do not
touch live keys. Remind me to turn on Studio access to API services. Test: earn coins, stop,
start again, confirm they are back. Screenshot the leaderboard after the rejoin.
This is the DataStore saving with session locking prompt; the button on that page opens Forge with it typed in. Forge reads your place first: if you already have ProfileService, ProfileStore or your own module, it keeps it and matches the API.
When ProfileStore cannot be installed
Some places block Creator Store inserts, and some developers want no third-party module. Forge's fallback (its datastores-fallback playbook) is a hand-rolled module with the same public API and a real lock:
- Lock on load. Loading is an
UpdateAsyncwhose transform writesSessionId = game.JobIdandLastUpdate, and cancels (returns nil) when another server's lock is younger than about 150 seconds. - Check the lock on every save. The save transform cancels when
old.SessionIdis not this server. A server that lost its lock never overwrites newer data. - Release on leave (
SessionId = nil) so the next server loads at once; autosave every 60 seconds refreshesLastUpdateso a crashed server's lock expires. - Never
UpdateAsync(key, function() return data end). That is a blind overwrite, the same asSetAsync.
Five checks for any AI-written DataStore script
- Search the place for DataStoreService. It should appear in exactly one module. A second script with
GetAsyncis a dupe waiting to happen. - Find the lock. ProfileStore's
StartSessionAsync, or anUpdateAsynctransform that writes a session id and cancels on a foreign lock.GetAsyncfollowed bySetAsyncis not a lock. - Look for pcall. Every store call is wrapped, with a
warnthat names the player and the reason. A silent failure is worse than a loud one. - Check the template. It has every field the game saves, including
PurchaseIdCache, and nothing that is not plain data. - Run the rejoin test with API access on. Earn, stop, start, confirm. Then run it with two clients and a trade or purchase, and confirm one copy of everything.
Forge finishes the saving step with that rejoin test and a screenshot, not just a clean console. If you are checking code from a script generator instead, the AI script generator guide has the wider checklist.
What it costs
Saving is one system-sized message: about 120 to 450 credits ($1.20 to $4.50) on Sonnet 5.5 including the rejoin test, per the pricing page ranges. The ProcessReceipt tie-in for developer products is a separate step of 100 to 400.
Questions
Why does plain GetAsync and SetAsync duplicate items?
GetAsync takes no lock. If a player is on server A, trades a pet, and server B (a rejoin after a crash, or a teleport) loads the same key before A saves, both servers now hold a copy. Whichever saves last wins, and the pet exists twice or the trade is undone. A session lock makes the second load wait or cancel.
Is ProfileStore safe to use from the Creator Store?
ProfileStore by loleris is the standard choice and the Creator Store copy is from the verified creator. Forge inserts it by asset id rather than retyping 65 KB of module, checks it arrived as a ModuleScript, and never edits it.
I already use ProfileService or my own module. Will AI replace it?
It should not, and Forge does not. It reads the place first; an existing data module is kept and its API matched. Migrating a live game's saves to a new store or key format is something to ask for explicitly, after a backup.
Why must the purchase id be saved with the data?
ProcessReceipt should return PurchaseGranted only after the grant and the receipt id are in the same saved profile. A separate purchase-history store can succeed while the data save fails, losing a paid purchase, or the reverse, granting twice on Roblox's retry.
Related guides
- Game pass and developer product scripts with AI: the ProcessReceipt that depends on PurchaseIdCache.
- Roblox leaderstats script with AI: the economy module that reads and writes this data.
- RemoteEvent security with AI: the handlers that call PlayerData.WaitFor.
- DataStore saving prompt: the one-click version of the prompt above.