New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

What is wrong with my destroy everything with whitelist function?

Asked by 5 days ago
local function DestroyAllWithWhitelist(Children,Table)
    for i,v in pairs(Children) do
        for k,d in pairs(Table) do
            if v.Name ~= d then
                v:Destroy()
            end

        end
    end
end

DestroyAllWithWhitelist(game.ServerStorage:GetChildren(),{'Fire',"Part",})

it does not work for some reason does anyone know why?

1
Hey not trying to be rude but, people has been answering your previous problems, wouldn't it be nice if you at least respond to them or give them an answer mark? Azure_Kite 498 — 5d
0
I wish so too. chloe_price65 164 — 5d
0
are you trying to destroy something if it's name does not exist in the whitelist? kisty1 7 — 5d

2 answers

Log in to vote
0
Answered by
hexwulf 271 Moderation Voter
4 days ago
Edited 4 days ago
local DestroyAllWithWhitelist = function(Entity, Tab) -- Args
     for _,obj in next, Entity do -- Iterating through entity
          for _,tab in next, Tab do -- Iterating through whitelist table
          if obj.Name == tab then -- if matching to entity in whitelist table
              obj:Destroy() -- destroys the matching entity.
             end
          end
     end
end

DestroyAllWithWhitelist(workspace.Baseplate:GetChildren(), {'Fire', 'Part'})

Learn from this, goodbye.

Ad
Log in to vote
-1
Answered by 5 days ago
Edited 5 days ago

Well here's what the script thinks.

As it runs through the objects destroying them one by one. In the second for loop, comes across a part named "Fire". So since the name of the part is named "Fire" it does not destroy it. However, on the second key/index, it comes across a string called "Part". Since the name of the part is named "Fire" and not "Part" it gets destroyed.

I would consider

--// Assume this is in the first for loop scope.
local Found_Value=table.find(Table,v.Name);
if not Found_Value then
    v:Destroy()
end;

Answer this question