How to add table in table like this:

local table = {
	[123] = {
		["UserName1"] = "abc"
	}
}

table = table + table[123] = { ["UserName2"] = "HELLO" }
print(table)
-- {
	[123] = {
		["UserName1"] = "abc",
		["UserName2"] = "HELLO"
	}
}

Please, if you want to share some code, make sure to format the code using `````` :slight_smile:

That’s happening because the table 123 already exists. You need to use another index to create another table.

If you want to insert a key to a table, you can do table.x = y. Here, that could look like table[123].UserName2 = "HELLO". If you wanted to insert multiple keys at once, you’d need a loop.

1 Like

He’s not creating another table, look at the result he wants:

2 Likes

Just create another key in the dictionary with the value you want

local tbl = {
	[123] = {
		["UserName1"] = "abc"
	}
}

tbl[123]["UserName2"] = "HELLO"

print(tbl)
--[[ {
	[123] = {
		["UserName1"] = "abc",
		["UserName2"] = "HELLO"
	}
}]]--

@rogchamp also mentioned another method via the dot operator, using square brackets and the dot operator are basically the same, the first method is safer if you have keys that are full numbers, such as yout 123 key

1 Like