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.
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 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)
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.
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.