Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Data Stores and Services

DataStore Limits, UpdateAsync and Safe Updates

Go beyond SetAsync with UpdateAsync, request limits, session locking against duplicate saves, OrderedDataStore leaderboards and safe patterns for data that must never be lost.

A basic save system works for a small game. Once many players join and several servers run at the same time, new problems appear: two servers saving the same player, requests being throttled, and numbers that must not be duplicated or lost. This lesson covers the tools and habits that keep data safe at scale.

As before, the Roblox code is shown read-only, and the runnable code is a JavaScript stand-in that models the logic. The exact limit numbers are published in the Roblox documentation and can change, so this lesson explains the ideas, not the figures.

Request limits and budgets

Roblox limits how many DataStore requests an experience can make per minute. The allowance grows with the number of players in the server. If you exceed it, requests are queued and may fail, and Roblox prints a warning.

You can check what is left:

LUA
local budget = DataStoreService:GetRequestBudgetForRequestType(
	Enum.DataStoreRequestType.GetAsync
)
print("Remaining GetAsync requests:", budget)

Good habits that keep you under the limit:

  • Cache data in memory and write only on leave, shutdown and a slow autosave.
  • Do not read the same key repeatedly. Read once, keep the result.
  • Combine many small values into one table under one key.
  • Never save in a tight loop, and never save on every property change.
  • Write requests to the same key have an extra, stricter cooldown (about six seconds between writes), so spamming one key is the fastest way to hit a wall.

SetAsync versus UpdateAsync

SetAsync blindly replaces the value. If two servers run it at nearly the same time, the later one wins and the earlier change is lost. UpdateAsync fixes this: you pass a function that receives the current saved value and returns the new value. Roblox retries the function if someone else changed the key in the meantime, so your update is always based on fresh data.

LUA
local ok, newValue = pcall(function()
	return store:UpdateAsync(key, function(oldData)
		oldData = oldData or { coins = 0 }
		oldData.coins += 50
		return oldData
	end)
end)

Rules for the callback: keep it fast, do not yield (no task.wait), have no side effects such as giving items (it may run more than once), and return nil to cancel the update.

SetAsync or UpdateAsync?

SetAsync Replaces the value. Last write wins.
UpdateAsync Reads, changes, writes safely.
Shared data Use UpdateAsync when two servers can touch a key.

Gifts, trades and global counters must use UpdateAsync.

1 / 2

The lost update problem, in code

Imagine Ana is online on server A, and a friend sends her 100 coins from server B. Server B reads her 50 coins, adds 100, and writes 150. Meanwhile server A reads 50, she earns 10, and writes 60. Server B's gift is gone. Run the stand-in and see both approaches.

JS Lost updates versus UpdateAsync (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

SetAsync lost the 100-coin gift and ended at 60. UpdateAsync retried the conflicting change and reached the right total of 160.

Session locking

Another danger: a player quickly leaves one server and joins another before the first has finished saving. The new server loads old data, and the old server then saves over the new one. A session lock solves this: store a lockedBy field with the server's id (game.JobId) when a session starts, refuse to load data locked by another live server, and clear the lock when saving on leave.

LUA
local ok, data = pcall(function()
	return store:UpdateAsync(key, function(old)
		old = old or { coins = 0 }
		if old.lockedBy and old.lockedBy ~= game.JobId and os.time() - old.lockTime < 120 then
			return nil -- another server owns this player right now
		end
		old.lockedBy = game.JobId
		old.lockTime = os.time()
		return old
	end)
end)

If data comes back nil, wait a few seconds and try again. The lock time lets a crashed server's lock expire so that players are never locked out forever. Well-known community modules implement this carefully; knowing the idea helps you pick and trust one.

JS A session lock with expiry (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Server B is refused while A owns the record, succeeds after A releases, and C only gets in after B's lock has expired.

OrderedDataStore for leaderboards

A normal DataStore cannot sort. An OrderedDataStore stores integer values and can return them sorted, which is exactly what a global leaderboard needs.

LUA
local board = DataStoreService:GetOrderedDataStore("TopCoins")

board:SetAsync("Player_" .. player.UserId, coins)

local pages = board:GetSortedAsync(false, 10) -- descending, 10 per page
local top = pages:GetCurrentPage()
for rank, entry in ipairs(top) do
	print(rank, entry.key, entry.value)
end

Update the board occasionally (for example when a player leaves), and refresh the display every minute or so, not every second.

JS A sorted leaderboard (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

More safety habits

  • Version your data. A version field lets you migrate formats later.
  • Use DataStore versions. Roblox keeps older versions of a key for a while, so with ListVersionsAsync and GetVersionAsync you can restore a player whose data was corrupted.
  • Do not store secrets or large blobs. Keep values well within the size limit.
  • Test failure. Turn off the network in Studio or throw errors on purpose to confirm your game does not wipe saves.
  • Respect privacy laws. If a user asks for deletion, RemoveAsync their keys, and avoid storing personal information.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem does UpdateAsync solve compared with SetAsync?

    UpdateAsync gives your function the latest value and retries if another server changed it, so concurrent changes are not overwritten.
  2. 2. Which rule applies to the function you pass to UpdateAsync?

    Because Roblox may retry the callback, side effects like giving items could happen twice, and yielding is not allowed.
  3. 3. What is a session lock for?

    A lock stores which server owns the record, so an old server cannot overwrite a newer session.
  4. 4. Which store gives you a sorted global leaderboard?

    OrderedDataStore holds integer values and offers GetSortedAsync to return them ranked.

Summary

  • DataStore requests are limited; cache in memory, batch values under one key and avoid frequent writes to the same key.
  • SetAsync can lose concurrent changes; UpdateAsync applies your change to the current value and retries on conflict.
  • A session lock with an expiry time keeps two servers from overwriting each other.
  • OrderedDataStore and GetSortedAsync build global leaderboards.
  • Version your data, use stored versions for recovery, and test what happens when requests fail.

Next: The Client-Server Model