Table Help in a Script

local test = {
{
	Name = "idk",
	Count = 1
},
{
	Name = "idk",
	Count = 1
}
}

How can I get all of it in a script? Like print each name and count.

1 Like

Either you use recursive function or double for loop for each table.

For loop example – catches even entries outside subtables!

for i, v in pairs(test) do
    if type(v) == "table" then
        for j, k in pairs(v) do
            print(j, k)
        end
    else
        print(i, v)
    end
end
3 Likes

try this

for i = 1 , #test do
    for i_ = 1 , #test[i] do
      print(test[i][i_])
   end
end

That is an example of numeric for loops, it is practical as well and only differ slightly in performance. It would probably count as a micro-optimization if I reckon that correctly.

An example of a recursive function (as stated by @anon81993163).
As you said; prints index/key and value.

local tbl = {
	{
		val1 = "val";
		val2 = "anothaval";
	},
	{
		yeah1 = "woohoo";
		yeah2 = "woohoooo";
		yeah3 = "oh";
	},
	{
		{
			"i'm inside a table!";
			"look at me!"
		},
		{
			val28 = "mhm works";
			val38 = "can confirm";
		}
	}
} -- random table lol

local function printAllInTable(t)
	for i, v in pairs(t) do
		if type(v) == "table" then
			printAllInTable(v)
		else
			print(i.." = "..v)	
		end
	end
end

printAllInTable(tbl)

Works fine.
image

Same code with your table.
image

1 Like