How to keep data store save retries in order
A retried save can land after a newer one and put given-away items back. How to queue saves per key so a retry never writes older data.
What it looks like
A failed save waits and tries again with the same value. Nothing stops the next save of that key from starting during the wait, so the older retry can land after the newer save and put old data back.
local snapshot = table.clone(data) for attempt = 1, 5 do if pcall(store.SetAsync, store, key, snapshot) then break end task.wait(2 ^ attempt) end
What an exploiter could do
An autosave fails while the player still has an item, and waits to retry. The player trades the item away, and the trade's save or the leave save succeeds. Then the old retry lands and writes the item back. The player rejoins with it while the other player keeps theirs. The same thing can happen by accident when data stores are slow or throttled, undoing purchases and progress.
How to fix it
- Give each key one queue. A new save for a key waits until the save before it, retries included, has finished. Roblox's sample
DataStoreWrapperdoes this. - Have each attempt write the latest in-memory data rather than a copy taken when the save began, so a retry can never be older than what's in memory.
- Or keep a save counter in the record, starting from the value loaded with the data. Increase it for every save, and return
nilfrom theUpdateAsynccallback if the stored counter is already at or above the one being written.
Vulnerable example
local DataStoreService = game:GetService("DataStoreService") local HttpService = game:GetService("HttpService") local Players = game:GetService("Players") local store = DataStoreService:GetDataStore("Backpacks") local backpacks: { [Player]: { string } } = {} local function save(player: Player) local backpack = backpacks[player] if not backpack then return end local encoded = HttpService:JSONEncode(backpack) for attempt = 1, 5 do local ok = pcall(store.SetAsync, store, tostring(player.UserId), encoded) if ok then return end task.wait(2 ^ attempt) end end Players.PlayerRemoving:Connect(function(player) save(player) backpacks[player] = nil end) task.spawn(function() while true do task.wait(60) for player in backpacks do task.spawn(save, player) end end end)
Fixed example
local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local store = DataStoreService:GetDataStore("Backpacks") local backpacks: { [Player]: { string } } = {} local saving: { [Player]: boolean } = {} local function save(player: Player, backpack: { string }) while saving[player] do task.wait() end saving[player] = true for attempt = 1, 5 do local ok = pcall(function() store:UpdateAsync(tostring(player.UserId), function() return table.clone(backpack) end) end) if ok then break end task.wait(2 ^ attempt) end saving[player] = nil end Players.PlayerRemoving:Connect(function(player) local backpack = backpacks[player] backpacks[player] = nil if backpack then save(player, backpack) end end) task.spawn(function() while true do task.wait(60) for player, backpack in backpacks do task.spawn(save, player, backpack) end end end)
How to spot this in your own scripts, and the cases that only look like it, are part of the Luaudit check your AI runs.