Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Should I use SetAsync or UpdateAsync?

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)

1 answer

✓ Accepted answer

So, UpdateAsync() updates the existing values to the new values with respect to them. But, if you want a whole new data, and existing values are useless in updating, then use SetAsync() to overwrite the data.

Looking at your code, it seems like you can use UpdateAsync().

2 comments

1

Thanks, I got it

0

Glad to hear that!

This discussion is preserved as it was. New answers, comments, and votes are closed.