Skip to content
Back to skills

Roblox Datastores

ASecurity

Safe player data persistence in Roblox — DataStore retry/backoff, session locking, autosaves, BindToClose, schema versioning and migration. Use when writing or reviewing any code that touches DataStoreService, player profiles, saving/loading, or data loss bugs.

  • 14 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 5, 2026
ai-agentsrustgosecurity

Works with

  • cli

Security analysis

A100/100

Scanned December 5, 2026

npx -y skills add ivar-anon/roblox-dev --skill roblox-datastores --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Roblox Datastores?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Roblox Datastores
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ivar-anon-roblox-datastores/badge)](https://www.skillsdirectory.com/skills/ivar-anon-roblox-datastores)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: roblox-datastores
description: Safe player data persistence in Roblox — DataStore retry/backoff, session locking, autosaves, BindToClose, schema versioning and migration. Use when writing or reviewing any code that touches DataStoreService, player profiles, saving/loading, or data loss bugs.
---

# Roblox DataStores: Data Loss Is a Design Failure

Players quit games forever over lost progress. DataStore calls fail routinely
(throttling, outages, shutdowns mid-write), and naive save code *will*
eventually dupe items or wipe saves. Treat persistence as a system, not a
`SetAsync` call.

## Non-Negotiables

1. **Every DataStore call goes through `pcall` with retry + exponential
   backoff.** No exceptions.
2. **Read-modify-write uses `UpdateAsync`, never `GetAsync` + `SetAsync`.**
   `UpdateAsync` receives the latest stored value and lets you transform it
   atomically; Get+Set races against other servers and against yourself.
3. **One server owns a player's data at a time (session locking).** Without
   it, a player rejoining a different server before the old server's final
   save lands will dupe or lose data. This is the #1 cause of item duplication
   exploits — players trigger it deliberately.
4. **Save on: autosave interval, player leave, and `game:BindToClose`.**
   All three. Servers die without warning; `BindToClose` gives you ~30
   seconds to flush everyone.
5. **Store only JSON-encodable data.** No Instances, no CFrames, no mixed-key
   tables, no cyclic tables, no non-string keys alongside array parts. Convert
   at the boundary.

## Recommended: Use a Battle-Tested Library

For production games, prefer **ProfileStore** (successor to ProfileService) —
it implements session locking, retries, and release semantics correctly.
These patterns below are what such a library does for you; implement them by
hand only when you have a reason to.

## Retry with Backoff

```lua
--!strict
local function withRetry<T...>(attempts: number, fn: () -> T...): (boolean, T...)
	local delaySeconds = 1
	for attempt = 1, attempts do
		local results = table.pack(pcall(fn))
		if results[1] then
			return true, table.unpack(results, 2, results.n)
		end
		warn(`DataStore attempt {attempt}/{attempts} failed: {results[2]}`)
		if attempt < attempts then
			task.wait(delaySeconds)
			delaySeconds = math.min(delaySeconds * 2, 16)
		end
	end
	return false
end
```

Budget awareness (limits as documented in late 2026):

- **Per server**: reads and writes each get `60 + 40 × players` requests per
  minute. `UpdateAsync` spends a read *and* a write. Check
  `DataStoreService:GetRequestBudgetForRequestType(...)` before non-critical
  writes and defer when low.
- **Per key, across all servers**: 4 MB/min written, 25 MB/min read. Throttled
  calls wait in a 30-slot queue; when it fills they fail outright, so a
  "save on every change" loop turns into dropped saves, not slower ones.
- Design so each player maps to one key that is written on a cadence, never in
  a loop.

## Session Locking (the concept)

On load, atomically claim the profile via `UpdateAsync`:

- If the stored data has no active lock (or the lock is stale — older than,
  say, 2× the autosave interval), write your server's `JobId` + timestamp into
  a `lock` field and proceed.
- If another server holds a fresh lock, **do not load**. Retry a few times
  (the other server may be mid-release), then kick the player with a friendly
  "your data is still saving, rejoin in a moment" message. Loading anyway is
  how dupes happen.
- On leave/shutdown: final `UpdateAsync` writes data AND clears the lock.
- Every autosave refreshes the lock timestamp, so a crashed server's lock goes
  stale and self-heals.

## The Profile Lifecycle

```text
PlayerAdded ──▶ claim lock + load ──▶ in-memory profile (single source of truth)
                                          │  gameplay mutates ONLY this table
                                          ├─ autosave every 30–60s (staggered per player)
PlayerRemoving ─▶ final save + release lock
game:BindToClose ─▶ final save + release for EVERY remaining player, wait for all
```

Rules that fall out of this:

- Gameplay code never touches DataStores directly — it reads/writes the
  in-memory profile table. Persistence is one module's job.
- Stagger autosaves (`player joins → offset = hash(UserId) % interval`) so 50
  players don't all save on the same tick.
- In `BindToClose`, save players in parallel (`task.spawn` per player), then
  wait until all finish or ~25s elapse. In Studio, skip the wait
  (`RunService:IsStudio()`), or stopping playtests becomes painful.
- `UpdateAsync`'s transform **cannot yield** and **may run several times**
  (Roblox re-calls it with the newer value on conflict). Keep it pure: no
  `task.wait`, no granting items, no remote fires inside it. Do side effects
  after the call succeeds.
- Return `nil` from an `UpdateAsync` transform to abort the write (e.g. the
  stored data is newer than what you have).

## Schema Versioning and Migration

Data outlives your code. Version it from day one:

```lua
local CURRENT_VERSION = 3

local DEFAULT_PROFILE = {
	version = CURRENT_VERSION,
	coins = 0,
	inventory = {},
	settings = { music = true },
}

local MIGRATIONS: { [number]: (any) -> any } = {
	[1] = function(data) data.settings = { music = true } return data end,
	[2] = function(data) data.inventory = data.items or {} data.items = nil return data end,
}

local function migrate(data: any): any
	while data.version < CURRENT_VERSION do
		local step = MIGRATIONS[data.version]
		if not step then error(`missing migration from v{data.version}`) end
		data = step(data)
		data.version += 1
	end
	return data
end
```

- Run `migrate` on load, inside the same `UpdateAsync` that claims the lock.
- Never delete fields in place across a release without a migration — old
  servers and new servers run simultaneously during a rolling update.
- Deep-copy `DEFAULT_PROFILE` for new players (a shared table reference will
  corrupt across players).

## Value Limits and Serialization

- A key's value is capped at 4,194,304 characters of serialized JSON, and the
  whole experience has a storage quota (500 MB + 1 MB per lifetime user, latest
  versions only). Profiles rarely hit either, but unbounded arrays (logs, full
  match histories) will. Cap or rotate them.
- Store, key and scope names are limited to 50 characters.
- Serialize special types at the boundary: `CFrame` → 12 numbers or
  position+orientation, `Color3` → hex string, `Vector3` → `{x, y, z}`,
  enum → `enum.Value`.
- Reject/strip `nil` holes in arrays before saving (JSON truncates at the
  first hole).

## What NOT To Do (review red flags)

| Red flag | Why |
| --- | --- |
| `SetAsync` for player data anywhere | Races; use `UpdateAsync`. |
| Saving inside gameplay events ("save on every coin pickup") | Throttled key writes; autosave instead. |
| `game:BindToClose` missing | Server shutdown wipes the last minutes of every session. |
| Data loaded without session lock, "we'll be fine" | Item dupes, discovered by players before you. |
| Storing the whole leaderstats folder / Instances | Not serializable; save plain tables and rebuild UI. |
| One giant DataStore key for all players | Size limit + write throttling + total loss blast radius. |
| Trusting client-sent data into the profile | See the roblox-security skill: validate first. |
| Wiping `nil` results as "new player" without checking the error | A failed `GetAsync`/`UpdateAsync` read looks like a new player if you don't distinguish "no data" from "call failed". Kick on load failure rather than starting a fresh profile over live data. |

That last one deserves emphasis: **if the load fails, do not let the player
play with a default profile** — their next save overwrites the real data.
Kick with a "data couldn't load, please rejoin" message.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.