Append data to dictionary without overwriting everything

I want to append something to a dictionary without having to completely overwrite it.

For example, to update this random datastore I would do this:

local Data = {
    ["A"] = true,
    ["B"] = false,
    ["C"] = {
        ["D"] = {
            ["E"] = true
        },
        ["F"] = false
    }
}
DataStore:SetAsync(Plr.UserId, Data)

This, however, overwrites all previously data saved on the DataStore.
How can I only update the key Data.C.D.E, and not overwrite all other keys?

Make a DefaultData table. Loop through the DefaultData, and check if player has each “type” of defaultData in their Data, if they don’t, add it to their data.

Something similar to this

local DefaultData = {
    ["A"] = true,
    ["B"] = false,
    ["C"] = {
        ["D"] = {
            ["E"] = true
        },
        ["F"] = false
    }
}


-- Lets say our player only have A & B for now.
local PlayerData = --PlayersData dictionary here
for DataName, DataValue in pairs(DefaultData) do
    if not PlayerData[DataName] then
        PlayerData[DataName] == DataValue
    end
end

If that wasn’t what you meant, please let me know and I can try and help you out.

1 Like

This is what I mean, but there’s a problem with this and I can’t think of a solution for it.
Dictionaries in dictionaries. The data I’m storing for the player will sometimes have 3-4 descendant dictionaries.

I would suggest adding a for loop for each dictionary descendant there is. I’m sure Kaiden is on the way with a solution.

1 Like

I accidentally pressed enter, my bad. What I was going to say, you could do something like this:

function gothrough(path, index, value)
    for i,v in pairs(path) do
        if i == index then
            v = value
        else
            if type(v) == "table" then
                gothrough(v, index, value)
            end
        end
    end
end

gothrough(data, "E", false)

Edit: I realized that you can’t change the value with just v, so here’s the fix:

function gothrough(path, index, value)
    for i,v in pairs(path) do
        if i == index then
            path[i] = value
        else
            if type(v) == "table" then
                gothrough(v, index, value)
            end
        end
    end
end

gothrough(data, "E", false)
2 Likes

If I’m correct, this function takes an outdated dictionary (path), changes any key named E (index), and sets it to false (value)

I’m sorry but my head just kind of explodes reading a function like that.

With the function, it goes through every table it has until it finds the key named “E.” Yes, it will change any key named “E,” but you could edit it and have some other specifics to define which “E” to change.

1 Like

It works! Since I’ll be using this in a separate module I still had to return the dict, but that’s easy to do.
Thank you, this is what I needed!

1 Like

You should be using UpdateAsync(). It will return the most recent data from the datastore and you can provide a transform function to update it accordingly. This way you have the most recent information and you’re able to change the specific index you need and return the data.