Can you create key/value pairs with values being a dictionary inside a dictionary in one step?

Hi there. So I am wondering if I can do this:

local list = {}


for i, v in ipairs(folderWithModels) do
	
	local mainBrick = v:FindFirstChild('Main')
	if mainBrick then
		local matMain = mainBrick.Material
		local clrMain = mainBrick.Color
		
		objectDefaults[v].Main = {
			['MainMaterial'] = matMain,
			['MainColor'] = clrMain
        }
  

to create a key named v’s name(The model name), which has a dictionary with the key Main which then has a value of a dictionary with 2 key/value pairs.

Would this work in Lua or do I have to do a table.insert?

edit: don’t worry about the formatting and missing ends

1 Like

Yes this works just fine, it’s just like doing this:

key = "value",
key2 = "other value"

table.insert or indexing with numbers creates an array like this:

"value",
"other value"

This is something that is pretty easy to test, you can just test it in studio and if any errors or bugs happen you can come here and get help.

1 Like

So even if objectDefaults is a a blank list, I can create v, Main, and the two keys all in one step?

You need to set anything that would be nil to a table before setting a key, but as long as you do that it all will work.

I don’t know if I understood correctly what you need, but this works

local folderWithModels = workspace.Folder
local objectDefaults = {}

for _, v in ipairs(folderWithModels:GetChildren()) do
	objectDefaults[v] = {}
	
	local mainBrick = v:FindFirstChild('Main')
	if mainBrick then
		local matMain = mainBrick.Material
		local clrMain = mainBrick.Color
		
		objectDefaults[v].Main = {
			['MainMaterial'] = matMain,
			['MainColor'] = clrMain
        }
	end
end

-- test
for model, subTable in pairs(objectDefaults) do
	print(model)
	if subTable.Main then
		for nameProperty, property in pairs(subTable.Main) do
			print(nameProperty, property)
		end
	end
end
1 Like