Destroy all the childrens of an element

I have a script in which there is an element variable (in this case “Backpack”). I want to take all the Childrens from the element and destroy them with -:Destroy()

2 Likes

Just do something like that

for i, v in pairs(game.Players["Player Name"].Backpack:GetChildren()) do
   v:Destroy()
end

This should remove all from backpack, and if you want to remove all tools even if any of them is equipped, use this

for i, v in pairs(game.Players["Player Name"].Backpack:GetChildren()) do
   v:Destroy()
end

for i, v in pairs(game.Players["Player Name"].Character:GetChildren()) do
   if v:IsA("Tool") then
      v:Destroy()
   end
end
2 Likes

Use the Instance’s Method ClearAllChildren() if it’s to Destroy EVERY Children instance in an Instance.
If it’s for deleting children with a condition, consider rtvr56565’s solution, which is using a loop and check if the children is a Tool.

Most of the time, Backpack are for Tools. It’s not recommended to put Scripts inside a backpack.

local Player = game.Players.SakyceWasTaken

Player.Backpack:ClearAllChildren()
6 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.