Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I use game:GetDescendants() with tables?

I'm making a anti virus so it deletes everything it finds in workspace with the viruses name, when I make it find one instance with the code below:

for key, virus in pairs(workspace:GetDescendants()) do

    if virus.Name == "Virus" then

        virus:Destroy()

    end

end

It works fine, but when I use tables like this:

local viruses = {"virus", "virus2"}

for key, virus in pairs(workspace:GetDescendants()) do
	if virus.Name == viruses then
		virus:Destroy()
	end
end

It does not work. Can anybody explain why and give me a fix? Thanks to anyone who replys.

1 comment

0

maybe try getting rid of the "

1 answer

✓ Accepted answer

Inside the loop, you would need to loop again through the viruses array to see if any of the children inside match it, for example


local viruses = {"virus", "virus2"}

for i,v in pairs(workspace:GetDescendants()) do
  for j,k in ipairs(viruses) do
    if v.Name == k then
        v:Destroy()
    end
  end
end

Sorry for improper tabbing, im on my phone

2 comments

0

Thanks! I'll use this.

0

Why would you need to loop through the table? Just use the table.find() method to check if 'v.Name' is inside the table, if it is, then destroy it.

This discussion is preserved as it was. New answers, comments, and votes are closed.