Adding a new table inside of a table

There have been other posts on this, but they don’t end up giving the answer I am looking for.
image

I am using profile service, and I am trying to add a new table to a profile. I can detect if there is no levels table already, but I need to be able to add a new table inside of the profile. How do I add this table inside of a table?

perhap :sweat_smile:

if levels = nil then
  data["Levels"] = {}
  levels = data["Levels"]
end

With ProfileService you should be using Reconcile to pull in new elements of the template to a player’s data structure. This way, ProfileService can add new keys from the template to a player’s data structure that might be missing the newly added key. A levels table should already be part of your template.

If for some reason Reconcile doesn’t work for you, then the above reply should resolve your question - you add a table inside of a table by doing exactly that.

2 Likes

I’m not 100% sure if this is what you’re looking for but you’re trying to ensure a table has all values, and if not you want to insert it?

I wrote a function that does this and relies on recursion so you can do it to any depth of a table, it takes 2 arguments: first argument is the table to be verified, or the table you want to insert values for, and the second table should be the table that is to be used to verify that the table to be verified, is verified if that makes sense.

So for example, let’s say I have this as my default table:

local defaultTable = {
    ['Coins'] = 0;
    ['OwnsCoolItem'] = false;
    ['Level'] = 0;
    ['SomeValue'] = 1234;
    ['SomeTableWhereValuesAreInserted'] = {
        ['number1'] = 1;
    };
}

Then the player joins, their data looks like this:

local playersData = {
    ['Coins'] = 12345;
    ['OwnsCoolItem'] = false;
    -- oh no the player's missing the Level value!
    ['SomeValue'] = 123456;
    ['SomeTableWhereValuesAreInserted'] = {
        ['number6'] = 123456;
        ['number2'] = 12345678;
        -- oh no they're missing an index number1!
    }
}

you can run it through this function and the output should be:

local playersData = {
    ['Coins'] = 12345;
    ['OwnsCoolItem'] = false;
    ['Level'] = 0;
    ['SomeValue'] = 123456;
    ['SomeTableWhereValuesAreInserted'] = {
        ['number6'] = 123456;
        ['number2'] = 12345678;
        ['number1'] = 1;
    }
}

Here’s the function:

local function verifyCurrentTable(tableToVerify, currentIteration) -- tableToVerify is where we would pass playersData, currentIteration is where we would pass defaultTable
	for i,v in pairs(currentIteration) do -- we have to use pairs because we're often working with dictionaries
		if tableToVerify[i] == nil then -- if inside the tableToVerify, it's missing a value then
			tableToVerify[i] = v -- insert the default value
		elseif type(v) == 'table' then -- if the value is a table, call this function but with the current table
			tableToVerify[i] = verifyCurrentTable(tableToVerify[i], v)
		end
	end
	return tableToVerify
end

It’s also important to note that if you’re ever to remove a value manually, it will keep reappearing due to it detecting a missing value.

Let me know if you need help with getting it set up, or if I completely overlooked what you’re looking for.

2 Likes