local Table1 = { Table2 = { Lion = 1, Cat = 2, Dog = 3, }, }
for i,v in pairs(Table1.Table2) do print("Table Table2 "..i,v) end
local new_table = { Lion = 6, Cat = 4, Dog = 9, }
for i,v in pairs(new_table) do print("Table new_table "..i,v) end
function updateTable(my_table, game_table) for i,v in pairs(my_table) do Table1.game_table[i] = v end end
updateTable(new_table, Table2)
for i,v in pairs(Table1.Table2) do print("New Table Table2 "..i,v) end
Line 5 gives me this error : attempt to index a nil value (field 'game_table') How can I modify the code for it to work? I'm not sure if I even need to use Table1 in the function, but Table1 is needed in what I'm trying to do, Table 1 will store multiple elements that are related to it.
You are calling Table2, but Table2 is nested inside of Table so try this:
local Table1 = { Table2 = { Lion = 1, Cat = 2, Dog = 3, }, } for i,v in pairs(Table1.Table2) do print("Table Table2 "..i,v) end local new_table = { Lion = 6, Cat = 4, Dog = 9, } for i,v in pairs(new_table) do print("Table new_table "..i,v) end function updateTable(my_table, game_table) for i,v in pairs(my_table) do Table1[game_table][i] = v end end updateTable(new_table, "Table2") for i,v in pairs(Table1.Table2) do print("New Table Table2 "..i,v) end
If you put game_table in brackets then it will use the value of game_table to find the key in Table1, now you can pass in "Table2" as a string
hey you! have you ever heard of enes? if you are in trouble, better call enes!