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

How do I print the names of tables?

Let's say I have this table:

local Rarities = {
	["Common"] = {"Oof1", "Oof2"},
	["Common2"] = {"Oofa", "Oofb"},
}

for i, v in pairs(Rarities) do
	print(v)
end

of course, this would print something like Table:9B9B9B9B

What I would like simply is to output "Common".

1 answer

✓ Accepted answer

If you simply want the indexes, then you should be printing i, not v If you needed to print in the values inside of i, then you would simply unpack it.

The reason why it is printing table's memory location is because

I) You are printing the value of index, not the index itself

II) Simply because you are printing the table, which if you wanted to get it's value, you would have to unpack it. You have a matrix. That is why.

local Rarities = {
    ["Common"] = {"Oof1", "Oof2"},
    ["Common2"] = {"Oofa", "Oofb"},
}

for i, v in pairs(Rarities) do
    print(i)
    print(unpack(v));
end

would print

--[[
Common2
Oofa	  Oofb
Common
Oof1	  Oof2
]]

Edit: Whoops forgot Lua 5.1 uses unpack not table.unpack.

1 comment

1

Works like a dream! Thank you!

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