For leaving the game should I use updateasync, or setasync?

If a player leaves the game what would I do if I wanted to save all their stat data to 1 key? Should I use updateasync? The documentation says that if I use setasync then there could be situations where a player joins 2 servers at once and it causes overwritten data. But updateasync reads it apparently automatically?

Here’s a portion of my script I’m planning on using for updating their data. It’s not completely finished yet though so don’t laugh at me PLEASE:

function module:UpdateData(DataKey, NewData)
	GameData:UpdateAsync(function(Key, transformFunction)
		return NewData
	end)
end

function module.PlayerLeft(Player)
	if Player:GetAttribute("DataLoaded") then
		local PlayerStatDataKey = Player.UserId .. "-stats"
		DataManager:UpdateData(PlayerStatDataKey)
	end
end

game.Players.PlayerRemoving:Connect(module.PlayerLeft)

NOTE: I am aware that in this script I have not updated the data, and instead passed through a key.

2 Likes

Yeah I kind of figured my post would get ignored over the basic ones like part.Transparency += 1

2 Likes

Usually you would just use setasync

2 Likes

I understand, but Roblox’s documentation says that if you use UpdateAsync then it saves versions of data as well. Wouldn’t that be more beneficial?

And I’m pretty sure when the documentation said that SetAsync can overwrite data from when you leave and join another server, that they were referring to PlayerRemoving events that save data whenever the player exits the server.

2 Likes

I’d say use UpdateAsync, it’s a bit more complicated but from what i know, it makes data loss rarer.

1 Like

Yeah I think UpdateAsync is the way to go for when the player leaves the game. Because if I do SetAsync then if a player joins two servers fast enough they could overwrite data. This is not good. I may even need to look into SessionLocking in case item duping may still be a thing.

2 Likes