How to use UpdateAsync() instead of SetAsync()

Im making a game that involves players teleporting across different places. I’ve noticed that occassionally players will lose their save data. I normally use SetAsync until I read a post about how this is bad practice and I tried rewriting my script to use UpdateAsync instead but I got seriously confused.

What am I trying to do?

I have a folder of stats parented to the player that need to be saved.

What is the current script?

local function SaveData(ThePlayer, SaveSlot)

local StatsToSave = {}
local NewKey = "Player-ID:" .. ThePlayer.userId .. SaveSlot
print(NewKey.." datasaved")

for i,Stats in pairs(ThePlayer:FindFirstChild(Statsholder.Name):GetChildren()) do 

	table.insert(StatsToSave, Stats.Value)

end

DataBaseStore:SetAsync(NewKey, StatsToSave)

end

^ This is the current function being used to save data. How could I call this function to rewrite it using UpdateAsync?

Really simple, do this:

DataBaseStore:UpdateAsync(NewKey, function()
return StatsToSave
end)

We’re returning the data we want to save. The new key is the key the stats are saving in.

2 Likes

Wow!!! Im amazed actually I feel silly. Well thank you very much!!

1 Like