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.
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.
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.