How to give items to players whose data another server holds

Writing straight into a session-locked profile is overwritten by the server that holds it. How to use an inbox key that the player's own server applies.

warningLuaudit check: writes-around-session-lock

What it looks like

Player data is session-locked, but another feature writes straight into a player's profile key: a gift to a friend who may be offline, a staff grant, a refund or a compensation script. The write ignores the lock, so if the player is in another server, that server's next save replaces the change with its own copy.

Luau
profiles:UpdateAsync(tostring(friendId), function(stored)
	stored.profile.Coins += amount
	return stored
end)

What an exploiter could do

A gift written into a friend's profile while the friend plays in another server is overwritten by that server's next autosave. The sender has paid and the friend never gets it. A staff grant or refund disappears the same way. If the write drops the lock, a second server can load the profile while the first still has it in memory, which opens the same item duplication as Player data with no session lock.

How to fix it

  • Write changes for other players to a separate key, such as an inbox per recipient, and let the server that holds their session apply it to the in-memory profile, then save.
  • Take the sender's payment and add the inbox entry together. If the inbox write errors, read the inbox back without the cache before giving the payment back, since a write that errors may still have landed.
  • Give each inbox entry an id, and record the ids already applied in the profile, so an entry is never applied twice.
  • Use MessagingService only to tell the recipient's server to check the inbox sooner. It doesn't replace the saved entry.

Vulnerable example

Vulnerable
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

type Stored = {
	profile: { Gems: number },
	lock: string?,
}

local profiles = DataStoreService:GetDataStore("Profiles")
local sessions: { [Player]: { Gems: number } } = {}

ReplicatedStorage.Remotes.SendGems.OnServerEvent:Connect(function(player: Player, friendId: unknown, amount: unknown)
	if typeof(friendId) ~= "number" or friendId ~= math.floor(friendId) or friendId < 1 or friendId == player.UserId then
		return
	end
	if typeof(amount) ~= "number" or amount ~= math.floor(amount) or amount < 1 or amount > 500 then
		return
	end
	local session = sessions[player]
	if not session or session.Gems < amount then
		return
	end
	session.Gems -= amount
	local ok = pcall(function()
		profiles:UpdateAsync(tostring(friendId), function(stored: Stored?): Stored?
			if not stored then
				return nil
			end
			stored.profile.Gems += amount
			return stored
		end)
	end)
	if not ok then
		session.Gems += amount
	end
end)

Fixed example

Fixed
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

type Gift = {
	id: string,
	from: number,
	gems: number,
}

local inboxes = DataStoreService:GetDataStore("GemInboxes")
local fresh = Instance.new("DataStoreGetOptions")
fresh.UseCache = false
local sessions: { [Player]: { Gems: number } } = {}

ReplicatedStorage.Remotes.SendGems.OnServerEvent:Connect(function(player: Player, friendId: unknown, amount: unknown)
	if typeof(friendId) ~= "number" or friendId ~= math.floor(friendId) or friendId < 1 or friendId == player.UserId then
		return
	end
	if typeof(amount) ~= "number" or amount ~= math.floor(amount) or amount < 1 or amount > 500 then
		return
	end
	local session = sessions[player]
	if not session or session.Gems < amount then
		return
	end
	session.Gems -= amount
	local gift: Gift = { id = HttpService:GenerateGUID(false), from = player.UserId, gems = amount }
	local added = false
	local ok = pcall(function()
		inboxes:UpdateAsync(tostring(friendId), function(stored: { Gift }?): { Gift }?
			local list = stored or {}
			added = #list < 50
			if not added then
				return nil
			end
			table.insert(list, gift)
			return list
		end)
	end)
	if ok then
		if not added then
			session.Gems += amount
		end
		return
	end
	local checked, latest = pcall(inboxes.GetAsync, inboxes, tostring(friendId), fresh)
	if not checked or typeof(latest) ~= "table" then
		return
	end
	for _, entry in latest :: { Gift } do
		if entry.id == gift.id then
			return
		end
	end
	session.Gems += amount
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.