How do I make a script that will delete all children in a model except for one!?
function destroyAllBut(model, keep) for k, v in next, model:GetChildren() do if v.Name ~= keep then wait(2) v:Destroy() end end end destroyAllBut(game.Workspace.Model, "Banana");
First things first, the part or object you want to keep will have a unique name, assuming so anyway, so pass that as the second argument to the function.
The first argument is the path to your model.
:Destroy() will destroy any thing and it's children that doesn't share the name of the object you want to keep.
This will destroy everything in the model except objects that have the same name as the keep argument, as a result it is more useful than just keep all but one object.
If you do just want to keep any random one object use this.
function destroyAllButOne(model) num = #model:GetChildren() for k, v in next, model:GetChildren() do if k < (num - 1) then wait(2) v:Destroy() end end end destroyAllButOne(game.Worskpace.Model)
Destroy all but Random
function destroyAllButRandom(model) math.randomseed(tick()) local random = model:GetChildren()[math.random(0, #model:GetChildren())] for k, v in next, model:GetChildren() do if v ~= random then wait(2) v:Destroy() end end end destroyAllButRandom(game.Workspace.Model)
This script will destroy every child of a model except one. Make sure to point the mdl variable to the model you would like the script to use.
This removes every part of the model except one random one:
mdl = game.Workspace.Model items = mdl:GetChildren() for i = 1, #items - 1 do wait(2) items[i]:Destroy() end
This removes every part of the model except one you want to keep:
mdl = game.Workspace.Model keep = "Special" items = mdl:GetChildren() for i = 1, #items do wait(2) if(items[i].Name ~= keep) then items[i]:Destroy() end end
Well, You can get children. I'm sure alot of people wont use my method but here's some code:
model = game.Workspace.model -- What model? children = model:GetChildren() -- Get an array. dontremove = "bob" -- Name of the object that we wont remove for i = 1, #children do if children[i].Name ~= dontremove then --If the child's name isn't bob. children[i]:Remove() -- Remove it. end end
Here's a reference for the future: http://wiki.roblox.com/index.php?title=GetChildren_(Method)
In the script below, you can place multiple names in the 'Keepers' table, objects found inside the 'Instance' variable (obj) with a name that is found inside the table will not be deleted.
function tableFind(table, val) for _, v in pairs(t) do if v == value then return true end end return false end Keepers = { 'Part'; 'OtherPart'; -- Optional } Instance = Parent['ModelName'] for _,v in pairs(Instance:GetChildren()) do if not tableFind(Keepers, v.Name) then v:Destroy() end end
Hope this helps.
Closed as Not Constructive by evaera
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?