So I have been wondering if it is possible to add tables in DataStores. I already used SetAsync to set the first table in the DataStore, which is myTb. I removed SetAsync after that. Here is my code that I tried out:
local dataStore = game:GetService("DataStoreService") local gds = dataStore:GetDataStore("gds") local myTb = {{sName = "server", sNum = math.random(1, 9999999), sAvail = true, playersInQueue = {}}} local key = "figure" local f = gds:GetAsync(key) local x = f[1] for i, v in pairs(f) do print(i, v) end for i, v in pairs(x) do print(i, v) end local function updateAvailability(currentValue) local serverTable = currentValue[1] local serverName = serverTable["sName"] local serverNumber = serverTable["sNum"] local serverAvail = serverTable["sAvail"] local plrsInQ = serverTable["playersInQueue"] local updateServerAvailability = {{sName = serverName, sNum = serverNumber, sAvail = false, playersInQueue = plrsInQ}} return table.insert(f, updateServerAvailability) end local success, newVal = pcall(function() gds:UpdateAsync(key, updateAvailability) end) if success then wait(1) local log = gds:GetAsync(key) print(#log) end
At line 36, I am expecting the number of tables in the DataStore table will be two, however, the output says it is only one. Did I make any mistakes in my code?
on the wiki page for UpdateAsync
, it says that the update is cancelled when the function returns nil (aka nothing)
table.insert
doesn't return anything, so when you do return table.insert(f, updateServerAvailability)
, it just returns nil
what you have to do instead is return f
itself:
table.insert(f, updateServerAvailability) return f
if that doesn't make sense, then you should know that tables are each separate objects, like instances; if a and b have the same contents, that doesn't make them equal to each other, and if you set two variables to the same table, a change to one variable will also apply to the other