How to check if something is already in a Table

I’m trying to check if something is already in a table, and if it’s not, add it into the table, and I have no idea how to do it.

I have tried this:

if a.Name not in NumberOfSubStates then
     table.insert(NumberOfSubStates, a.Name)
end

And that wont work…

For an array (indices 1 and up) you can use table.find:

if (table.find(NumberOfSubStates, a.Name) == nil) then
	table.insert(NumberOfSubStates, a.Name)
end

If you’re using a dictionary, (using the name itself as an index) you can find it directly:

if (NumberOfSubStates[a.Name]) then
	NumberOfSubStates[a.Name] = true -- or whatever truthy value you would like to associate with the index
end
2 Likes