How do I select the first value of a valued string in a table

Hey, how would you select this in the script:

local tablee = {
   ["String"] = "Stone",
}

print(tablee[1]) -- Outputs Stone

Any way to get the “String” to output?

1 Like

printing this will result to nil, it should be print(tablee["String"] and it will return “Stone”

to get the “String” from the table, you can use a loop

for i, v in pairs(tablee) do
   print(i, v) -- 'i' will print "String", while 'v' will print "Stone"
end
1 Like

Is that guaranteed to return the first entry in the table?

Thanks, I thought the “i” returns index such as 1,2,3,4. Ig the index in the table is the string.

1 Like

If you want the 1st index and value to get the 1st item on the table, simply replace pairs into ipairs. Doing ipairs will loop through the table in the correct order, while pairs will loop in an unknown order (I think).

The “ipairs()” iterator can only be used to loop over arrays (tables with sequential number indices starting from 1, i.e; 1, 2, 3 etc.). It cannot be used to iterate over dictionaries (tables with custom indices, i.e; [“A”], [“B”], [“C”]).

If a table’s order matters then you must use an array, the dictionary in the example could be converted into an array of arrays.

local tables = {{"String", "Stone"}, {"String", "Wood"}}
2 Likes