Most "my players lost their data" bugs come from the same five DataStore mistakes. Below is a free module that avoids all of them. Copy it, use it, change it.

The 5 mistakes

  1. No pcall. DataStore requests fail sometimes. Without pcall, one failure errors your script.
  2. No retry. A single failed save means lost progress. Retry a few times with a short wait.
  3. Saving on every change. You hit the request limits and get throttled. Save on a timer, on leave, and on shutdown.
  4. Overwriting with SetAsync. Two servers can overwrite each other. Use UpdateAsync so you work on the latest value.
  5. Forgetting BindToClose. When a server shuts down, players still in game are not saved unless you handle it.

The free module

Put this in a ModuleScript named PlayerData in ServerScriptService.

local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local store = DataStoreService:GetDataStore("PlayerData_v1") local DEFAULTS = { coins = 0, level = 1 } local MAX_TRIES = 4 local PlayerData = {} local cache = {} local function withRetry(fn) local lastErr for attempt = 1, MAX_TRIES do local ok, result = pcall(fn) if ok then return true, result end lastErr = result task.wait(attempt) -- 1s, 2s, 3s between tries end return false, lastErr end local function copyDefaults() local t = {} for k, v in pairs(DEFAULTS) do t[k] = v end return t end function PlayerData.load(player) local key = "user_" .. player.UserId local ok, data = withRetry(function() return store:GetAsync(key) end) if not ok then warn("Load failed for " .. player.Name .. ": " .. tostring(data)) return nil -- do NOT save later, or you overwrite good data with defaults end cache[player] = data or copyDefaults() return cache[player] end function PlayerData.get(player) return cache[player] end function PlayerData.save(player) local data = cache[player] if not data then return false -- load never succeeded, so never save end local key = "user_" .. player.UserId local ok, err = withRetry(function() return store:UpdateAsync(key, function() return data end) end) if not ok then warn("Save failed for " .. player.Name .. ": " .. tostring(err)) end return ok end Players.PlayerAdded:Connect(PlayerData.load) Players.PlayerRemoving:Connect(function(player) PlayerData.save(player) cache[player] = nil end) -- autosave every 2 minutes task.spawn(function() while true do task.wait(120) for _, player in ipairs(Players:GetPlayers()) do task.spawn(PlayerData.save, player) end end end) game:BindToClose(function() for _, player in ipairs(Players:GetPlayers()) do task.spawn(PlayerData.save, player) end task.wait(3) end) return PlayerData 

How to use it

local PlayerData = require(game.ServerScriptService.PlayerData) local data = PlayerData.get(player) if data then data.coins += 10 end 

Change only server-side. Never let the client tell the server how many coins it has.

Two honest limits

  • This version does not add a session lock, so two servers can still hold the same player briefly. If duplication matters in your game (trading, rare items), add a lock. I wrote that part up separately.
  • Test in a published game with Studio API access enabled, since DataStores need it.

Want the rest?

This module is one piece of my Roblox Game Starter Kit: tested datastore, shop and leaderboard scripts, all server-validated. Launch code LAUNCH25 gives 25% off until Dec 17: Roblox Game Starter Kit. The module above is free whether or not you buy.