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

Help with a Table?

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

✓ Accepted answer

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 comment

1

Thanks.

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