Problem with recursion

Hello!

I’ve run into an issue with my datastores and using recursion. The problem is that I am creating a data verification system and I just cannot get it to work.

So, I have this template for default user data:

local defaultData = {
    ['Coins'] = 0;
    ['Wins'] = 0;
    ['Completed Obbies'] = 0;
    ['Inventory'] = {
        ['Accessories'] = {};
        ['Packages'] = {};
        ['Gear'] = {};
        ['Trails'] = {};
    };
    ['Rank'] = 'AlphaTester';
    }

When iterating through it, there are some nested tables. The problem arises when I get to the nested table. The data properly saves but when I try to verify, it just overwrites with a blank table.

function verifyData(tableToVerify, currentIteration)
        local returnedTable = {}
        for i,v in pairs(currentIteration) do
            if tableToVerify[i] then
                if typeof(v) == 'table' then
                    returnedTable[i] = verifyData(v, currentIteration[i])
                else
                    returnedTable[i] = tableToVerify[i]
                end
            else
                returnedTable[i] = v
            end
        end
        return returnedTable
    end
    local dataFolder = Instance.new('Folder', player)
    dataFolder.Name = 'PlayerData'
    
    local newData = verifyData(data, defaultData)

Saving and loading the data both properly work, it’s just verification.

So the index of each iteration should be the value’s name, and the value should be the stringvalue’s value.

Capture d’écran, le 2020-12-20 à 23.22.33

After rejoining:

Capture d’écran, le 2020-12-20 à 23.23.15

The problem is that the values that are the direct children of PlayerData save, it’s just the children of the nested folders that don’t.

TIA for any help!

Found an answer after way too much trial and error:

function verifyData(tableToVerify, currentIteration)
        local tableToReturn = {}
        for i,v in pairs(currentIteration) do
            if tableToVerify[i] then
                print('exists')
                tableToReturn[i] = tableToVerify[i]
                if typeof(tableToVerify[i]) == 'table' then
                    tableToReturn[i] = tableToVerify[i]
                    verifyData(tableToReturn[i], v) -- verify the RETURNED table instead of the current table which has to be verify
                else
                    tableToReturn[i] = tableToVerify[i]
                end
            else
                tableToReturn[i] = v
            end
        end
        return tableToReturn
    end
1 Like