Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with a Table?

Asked by
TopDev 0
10 years ago

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.

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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)

1
Thanks. TopDev 0 — 10y
Ad

Answer this question