How to see if part name is in table

local table = {“hi”,
“icecream”,
“soda”,
“bloxy soda”,
“WOW”,
“Fruitloops”,
“Hey”,
}

local descendants = game.Workspace:GetDescendants()

for index, descendant in pairs(descendants) do

		if descendant.Name == table then
			descendant:Destroy()
			print("Removed A Threat")
		end
		
	end

You can simply use table.find(YourTable, string/Instance that you want to find).

1 Like

yes but i have the table list of names i want to destroy the part if it has that name

1 Like

@XXgamerisaliveXx has the right idea for this issue. Here is the code for a better understand of how this is implicated. table.find() will return nil (false) if it doesn’t find a value and it will return a result (true) if it is found.


local table = {
“hi”,
“icecream”,
“soda”,
“bloxy soda”,
“WOW”,
“Fruitloops”,
“Hey”
}

local descendants = game.Workspace:GetDescendants()

for index, descendant in pairs(descendants) do
	if  table.find(table,descendant.Name) then
		descendant:Destroy()
		print("Removed A Threat")
	end
end
1 Like

Actually table.find() returns nil if it doesn’t find…

1 Like

nil is false when put through a if statement.

1 Like

but if you try to print print(table.find(table, Name)) and if it finds the value given then it returns its index in the table else nil.

1 Like

Yes, but as stated on Lua.org

consider false and nil as false and anything else as true

2 Likes

any other values returned as long at it isnt nil or false, is interpreted as true

1 Like

Yo this exists xd

workspace:FindFirstChild("Name", true) -- the 2nd arg means whether it's cursive or not so it goes thru descendants
1 Like

That’s for truthy and falsy values and implicit boolean conversions, implying that table.find returns literal false (equals to false) if it doesn’t exist is wrong.

table.find indeed returns nil if the item doesn’t exist. Don’t mix literal false and falsy values up.

2 Likes