Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you update values in a table with UpdateAsync?

Asked by 5 years ago

This code won't work, when you spawn and don't have existing data it will set a table of values for you. If you have data then it will update the data inside the table and add 10 to it but it won't work. It keeps overwriting everything and clearing the whole data saved within the key and making it '60' instead of the stats table.

How do you update table values in a table with UpdateAsync without having to copy and paste the whole table every single time and put all the tedious values in there again?


local DS = game:GetService('DataStoreService'):GetDataStore('1') wait(2) local data local key = '1' local success, ret = pcall(function() data = DS:GetAsync(key) end) if data then local stats = data.stats local coins = stats.coins local gems = stats.gems DS:UpdateAsync(key, function(prev) return stats.coins + 10 end) wait(1) for k, v in pairs(stats) do print(k,v) end elseif not data then DS:SetAsync(key, { stats = { coins = 50; gems = 10; } }) else error(ret) end

Answer this question