The developer wiki says that SetAsync shouldn't be used when updating a data store, and that UpdateAsync should be used instead. But what if I wanted to update it to new data?
Let's say I wanted to store a player's kills:
-- Correct me if there's something wrong with my code
local DataStoreService = game:GetService("DataStoreService")
local killsStore = DataStoreService:GetDataStore("PlayerKills")
local playerKills = nil
game.Players.PlayerAdded:Connect(function(player)
killsStore:SetAsync(player.UserId, 0) -- Pretend it checks if the key already exists before this
playerKills = killsStore:GetAsync(player.UserId)
player.Kills.Value = playerKills -- Kills is an IntValue here
end
And then I wanted to update the data store. I could do this:
local function getNewKills()
return playerKills
end
game.Players.PlayerRemoving:Connect(function(player)
playerKills = player.Kills.Value
killsStore:UpdateAsync(player.UserId, getNewKills)
end)
But if I'm just going to overwrite the previous data, why not use SetAsync instead?
game.Players.PlayerRemoving:Connect(function(player)
playerKills = player.Kills.Value
killsStore:SetAsync("Player_1234", playerKills)
end)

2 comments
Thanks, I got it
Glad to hear that!