How to see if a part has a name that is in a table

Is there a way i can check if a part has a name in the given table i cant get it to work. Here is the script

local Viruses = {“d��������������ng…you got owned…”,
“infected”,
“infected”,
“ProperGr�mmerNeededInPhilosiphalLocations;insertNoobHere”,
“Virus”,
“Virus”,
“VirusScript”,
}

local descendants = game.Workspace:GetDescendants()

for index, descendant in pairs(descendants) do

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

The easiest way to do this imo is something like

local Viruses = {"d��������������ng…you got owned…",
"infected",
"ProperGr�mmerNeededInPhilosiphalLocations;insertNoobHere",
"Virus",
"VirusScript",
}

local IsVirus = {}

for i,v in ipairs(Viruses) do
  IsVirus[v]=true
end

local Archive = Instance.new("Folder",game.ServerStorage)
Archive.Name="Threats"

for i,v in ipairs(workspace:GetDescendants()) do
  if IsVirus[v.Name] then
    print("Potential threat removed",v:GetFullName())
	v.Parent = Archive -- this way you're less likely to totally lose something by mistake
    --v:Destroy()
  end
end
1 Like

You can make use of table.find,

the first parameter is the table, and the second parameter will be the value.
If it can’t find the value in the table, It will return nil.

This is how it will look like to use on your script.

local Viruses = {“d��������������ng…you got owned…”,
“infected”,
“infected”,
“ProperGr�mmerNeededInPhilosiphalLocations;insertNoobHere”,
“Virus”,
“Virus”,
“VirusScript”,
}

local descendants = game.Workspace:GetDescendants()

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

Hey cool, I didn’t know table.find was a thing.
That might not be the best in terms of performance, but it probably won’t matter unless your list is very long.

table.find uses a linear search algorithm as it states in the API Reference, and if I am right it will check through each element of that list until it finds a match or the list has been totally searched.