DataStore is one of those things that works fine in Studio and then quietly bites you once real players show up. Here’s the checklist I’d run through before going live. Curious what others are doing too.
1. Load on PlayerAdded, not CharacterAdded
The sample in the docs loads inside CharacterAdded, which is fine for a demo. In a real game though, CharacterAdded fires again every time the player respawns, so you end up re-reading the stored value and overwriting whatever the player earned since the last save. Load once per session on PlayerAdded.
2. If the load failed, never save
This is the one that actually wipes progress. If GetAsync errors and you fall back to default values, the next save (on leave, or on autosave) writes those defaults over the player’s real data. Keep a flag that’s only set when the load succeeded, and skip saving for anyone without it.
3. One table per player, not one store per stat
Every key is a separate request, and the docs list a limit of 150 requests per minute. Gold + Level + Exp + Inventory as four stores means 4x the requests on every load and save. One table under one key (player.UserId) keeps it to a single call.
4. Autosave, don’t rely only on PlayerRemoving
The docs call out that data can be lost if a server ends unexpectedly. I couldn’t find BindToClose in the API reference, so I don’t count on a shutdown hook. A periodic autosave caps how much a player can lose. Every 1-2 minutes is plenty and stays far below the rate limit.
5. Wrap every call in pcall and retry with a short backoff
Saves and loads can fail for reasons outside your control. One retry loop shared by everything keeps this tidy.
Putting it together (Script in ServerScriptService):
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerStore = DataStoreService:GetDataStore("PlayerData_v1")
local DEFAULT_DATA = { Gold = 0, Level = 1, Exp = 0 }
local MAX_RETRIES = 3
local AUTOSAVE_INTERVAL = 120
local sessionData = {} -- [userId] = data table
local loadedOk = {} -- [userId] = true only if the load succeeded
local function withRetry(fn)
local lastErr
for attempt = 1, MAX_RETRIES do
local ok, result = pcall(fn)
if ok then
return true, result
end
lastErr = result
task.wait(attempt * 2)
end
return false, lastErr
end
local function onPlayerAdded(player)
local userId = player.UserId
local ok, stored = withRetry(function()
return PlayerStore:GetAsync(userId)
end)
if not ok then
warn("Load failed for " .. player.Name .. ": " .. tostring(stored))
return -- not marked as loaded, so we'll never overwrite their real data
end
-- Start from defaults so newly added fields get a value for existing players
local data = {}
for k, v in pairs(DEFAULT_DATA) do
data[k] = v
end
if type(stored) == "table" then
for k, v in pairs(stored) do
data[k] = v
end
end
sessionData[userId] = data
loadedOk[userId] = true
player:SetAttribute("IsDataLoaded", true)
end
local function save(player)
local userId = player.UserId
if not loadedOk[userId] then
return
end
local ok, err = withRetry(function()
PlayerStore:SetAsync(userId, sessionData[userId])
end)
if not ok then
warn("Save failed for " .. player.Name .. ": " .. tostring(err))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(function(player)
save(player)
sessionData[player.UserId] = nil
loadedOk[player.UserId] = nil
end)
task.spawn(function()
while true do
task.wait(AUTOSAVE_INTERVAL)
for _, player in ipairs(Players:GetPlayers()) do
save(player)
end
end
end)
Game code then reads and writes sessionData[player.UserId] instead of touching the store directly. The IsDataLoaded attribute is handy for keeping a loading screen up until the data is ready, which the docs also recommend.
Two smaller gotchas
UpdateAsync return format changed in release-40. The callback now returns multiple values (return newValue, userIds, metadata) instead of a single table. If you still return { newValue, userIds, metadata }, the whole table gets saved as the value and GetAsync won’t read your original data anymore. Worth a quick search through older scripts.
Studio data doesn’t persist. In Studio tests, data is stored locally and deleted when the session ends. If you’re testing anything save-related across sessions, test on a published world.
Docs this is based on: Saving & Loading Data
How are you all structuring your saves? Especially interested if anyone’s done versioning or migrations for when the data shape changes between updates.