Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What can I do to print out content from a table inside another table and how can I do it?

Hello! I have a table for a project I'm working on. I am trying to read the data inside the table that has contents. If you want to do print(tablename[1]) for a table that's inside another table, you get "nil" in your output. Any solutions appreciated!

shorturl.at/inM06

1 comment

0

tbl1[index1][index2]

1 answer

There's a couple ways of doing this:

Option #1:

myTable = {
	{"Dogs", "Cats", "Bunnies"}, 
	{2,3,10}
}

print(myTable[1]) --prints the first table's pointer
print(myTable[1][2]) --print's cats inside of table 1

--loops through all the tables and prints out their values 
for index, innerTable in ipairs(myTable)do
	print(innerTable, " ", index) --prints which table we're looking at
	for itemIndex, item in ipairs(innerTable)do
		print(item, " ", itemIndex) 
	end
end

Option #2:

myTable = {
	Animals = {"Dogs", "Cats", "Bunnies"}, 
	SpecialNumbers = {2,3,10}
}

--This table has no indecies, so you cannot do "print(myTable[1])", but you can instead do


for i, v in ipairs(myTable.Animals)do
	print(v)
end

for i, v in ipairs(myTable.SpecialNumbers)do
	print(v)
end

Option #1 is nice when you want to loop through everything in the table, which is rarely the case when it comes to having tables inside of tables.

Option #2 is nice because instead of looping, you can instantly get the table inside myTable without any added steps, which is a lot quicker when you have a big game with a lot of tables.

This discussion is preserved as it was. New answers, comments, and votes are closed.