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

How do I find a string in a table?

I've been trying this for a while.

So I have a table that looks like this:

{"ITEM1","ITEM2"}

I'm attempting to one of these values but I am not sure how I should go about it. Any help?

4 comments

1

use a array

0

it is an array, that's the json decoded string.

0

json decode will return a dictionary, you can iterate/index them by following wiki https://www.robloxdev.com/articles/Table#iteration

0

arrays or loops will work... table[1] = "ITEM1" table[2] = "ITEM2"

1 answer

The code example you provided is an array table, the index would be 1 and 2 (sequential integers). If you want to find out if a value is in the table you'll need to iterate the table and compare the values to the one you're looking for.

local t = {"ITEM1", "ITEM2"}
for index, value in ipairs(t) do
	if value == "ITEM2" then -- let's say I want to find "ITEM2"
		print(value .. " found at index " .. index)
		return index
	end
end

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