I'm wondering if it is possible to get a table's position, like
Test = {"hello", "hi"}
hello would be Test[1]
I'm trying to make a script when a player dies, it will remove there name from the table.
So I would need to get the value or somehow remove the table by it's string name.
Lua's standard table library doesn't include a function already made that does this.
However, we can make one ourselves really easily using pairs
.
function tablefind(tab, val) for index, value in pairs(tab) do if value == val then return index end end end
This searches through all index
and value
pairs in the table tab
, and stops when it finds val
, and returns the appropriate index
.
In your example, tablefind(Test,"hello")
would return 1
.
This function upon not finding the value returns nil
(which is convenient since nil
cannot be a table index)