I want to know how I can tell if an object is in a table. I have no clue where to start trying to figure this out, so I ask, is there any good way to do this?
✓ ANSWER ACCEPTED
How can I detect if something is in a table?
1 answer
✓ Accepted answer
There are a few ways with dictionaries.
local myTable = {['object'] = true}
-- Version 1
local function findMy1(object)
if myTable[object] then
return true
end
return false
end
--[[ or shortened with
local function findMy1(object)
return myTable[object] -- > returns the value or nil if there isn't one
end
or using ternary operators
local function findMy1(object)
return myTable[object] and true or false --> returns a bool
end
--]]
-- Version 2 - For loop
local function findMy2(object)
for i,v in pairs(myTable) do
if i == object then
return true
end
end
return false
end
-- Version 3 Using a metatable and the metamethod __index to
-- give the table some function
setmetatable(myTable, {
__index = function(self, key)
print( string.format("%s was not found", key))
return rawget(self, key)
end
})
print ( findMy1('object'), findMy2('object'), findMy1('nilObject') )
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
You'd have to loop through the table and use 'IsA' to see if what was in the table was a part, or if you mean the bracket-thingies then
tbl['Thing']:IsA('BasePart')tablename[item] ~= nil
Either, MyTable["APig"] or loop the table and see if the value is found.