Datastore question

So, I have a question for datastores. I know how to save values that are inside of the player, but how would I save and load the table from the script. Let’s say
local table1 = {"Element1", "Element2", "Element3" }
Can I go with just

local data = {
     some values stuff, 
     some other stuff, 
     table1
}
mydataStore:SetAsync("key",data)
--------------------------------
data = mydataStore:GetAsync("key") 
if data then 
     table1 = data.table1
end

*This is just a concept, not real script"
would something like this work?

local data = myDataStore:GetAsync("key")
if data then
    data1 = data[1] -- if its a index-value (array)
    data2 = data["Coins"] -- if its a key-value (dictionary)
end

To explain how, it’s just how you get a value from a table (array and dictionary).

1 Like

You could also do the following for first-time players or times where :GetAsync returns nil:

local DataStoreService = game:GetService("DataStoreService");
local DEFAULT_DATA = {  --Assuming this is the sort of data you're storing
    Coins = 0,
    Gems = 0,
    Tools = {}
};
...  --Other stuff
local function Clone(myTable: {[string]: any}): {[string]: any}  --Just clones a table
    local t = {};
    for k,v in pairs(myTable) do
        t[k] = v;
    end
    return t;
end

local myDataStore = DataStoreService:GetDataStore("WhateverYourStoreIs");
local data = myDataStore:GetAsync("key") or Clone(DEFAULT_DATA);  --Either returns the saved data or a copy of the default data
1 Like