Printing only a sub tables index instead of the whole table

So I have a table looking something like this:

local table = {

[1] = {
Name = "Bob"
},

}

When I print “table[1]” then it prints the whole thingy including the name etc. How can I get it to only print the index? (the 1 for bob)

If you print (table) it should only print the contents of the table and that is [1].

edit: sorry if i am stupid, this is my first time replying in the dev forum

Well, I only want to get the index (1) not the name etc. It literally prints everything but not the index

local t = {
    {
        Name = "Bob"
        Index = 1
    }
}

print(t[1]["Index"])
1 Like

The problem is, you store a dictionary inside a dictionary ([1] = {}) but I think what you want to achieve is storing a dictionary inside an array and then getting the index of the dictionary in that array

SO:

local function getIndex(array,item)
    return table.find(array,item)
end

item would be you dictionary so {Name = “Bob”} and array would be {{Name=“Bob”} }

It has a reason why it’s a dictionary inside a dictionary. I need to have it like this

Why would you need it to only print the index?

Please clearify your question. What exactly is your issue and what exactly needs change?

idk what u mean but ig u can do this

local table = {

[1] = {
Name = "Bob"
},

} 

for _, v in ipairs(table) do for key, value in pairs(v) do print(key .. " = " .. value) end end 

prints Name = Bob as a string

if you mean you wanna just print the index (1) by itself you can do

for index, _ in pairs(table) do
print(index)
end

So what I’m doing really is looping through the “table” and creating a text button. I want to name the button accordingly (which has a reason) to the index which is also the id of the tables child. I need this number to do stuff with it later

Do it in another fashion:

local array = {
    { --will be item one
      Name = "Bob",
      ButtonText = "1"
    }
} 

And then loop:

for i, object in pairs(array) do
    button.Text = object.ButtonText.." : "..object.Name
end

Cant you just add another value to the index and use that value to create the button

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.