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

How do I find a string in a table?

Asked by
Shadrz 40
5 years ago
Edited 5 years ago

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?

1
use a array User#23365 30 — 5y
0
it is an array, that's the json decoded string. Shadrz 40 — 5y
0
json decode will return a dictionary, you can iterate/index them by following wiki https://www.robloxdev.com/articles/Table#iteration Vulkarin 581 — 5y
0
arrays or loops will work... table[1] = "ITEM1" table[2] = "ITEM2" greatneil80 2647 — 5y

1 answer

Log in to vote
0
Answered by
gullet 471 Moderation Voter
5 years ago

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
Ad

Answer this question