How can I update everything under a key in a key in my datastorage?

While you probably should use seperate keys for this, if you want to have it all under one key this is more or less how it would be done:
Simply get the previously saved data (which should be in a table/dictionary), then add the new data to the table and then just save it.

i.e.

dataStore:UpdateAsync(player.UserId .. "AdditionalInfo", function(old)
    local new = old or {} -- default value, in case they haven't saved data previously.
    if old["Created"] then                 -- If you have any existing data that is in the wrong format,
        new = {[old["Created"]] = old}  -- then change it so it is in the right format
    end

    new[dataToSave["Created"]] = dataToSave -- add the new data to the table
    return new
end)

How could I use separate keys? Since sometimes I want to pull everything from under this key, to display it in a big list.

Also, what about the pcall? do I do away with it?

pcall is important,

as for the code, as @SeargentAUS provided, you’d probably want to use :UpdateAsync(), as UpdateAsync() provides the previously saved data, allowing you to update the data rather than overwrite it

If you want to overwrite specific keys, it can look something like this (Similar to Seargent’s, but more than 1 table index can be modified at a time, and I’m not quite sure what is happening in his code)

local Success, Result = pcall(function()
    -- The return value of UpdateAsync isn't so useful...
    return dataStore:UpdateAsync(player.UserId .. "_AdditionalInfo", function(previousData)
        local Data = previousData or {}

        for i, v in pairs(dataToSave) do
            Data[i] = v -- Index i is overwritten with new data v
        end
    
        return new
    end)
end)

Only indexes present in dataToSave will be updated in the previously saved data. You can also, instead of overwriting the table index, you could add logic to instead take the old value and apply some modification to it, for example, if you are storing coins, instead of overwriting the amount of coins, you can keep track of the amount of coins earned or lost, and update coins by that amount

for i, v in pairs(dataToSave) do
    if i == "coins" then
        Data[i] = Data[i] + v -- Here v is the amount of coins gained or lost (if v is negative)
    else
        Data[i] = v -- Index i is overwritten with new data v
    end
end

Updating the amount of coins like this prevents data loss (from a user joining a server before the previous server the user was one could save the data), as the data for coins is never overwritten. It’s a method that isn’t so common, idk why

Having if statements like this isn’t ideal, there are better ways to structure it, but I wont get into that, although if you’d like to hear more, I’d be glad to share some techniques


I also probably don’t recommend using multiple keys. Not that it’s bad, but datastore keys can hold a lot of data, and having more keys increases the amount of datastore calls you have to make

To use multiple keys you’d basically store what currently is your table indexes as keys, for example player.UserId .. "_AdditionalInfo" .. Icon could be one key. To get all the data, you’d have to do multiple calls to GetAsync()

1 Like

Keep the pcall.

If you want to display everything in a big list, it’s probably acceptable to store it all in the one key as you wanted.

Thank you guys so much!

30char

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.