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

How do I make a script that will delete all children in a model except for one!?

How do I make a script that will delete all children in a model except for one!?

1 comment

0

Also i dont want it to delete them all at once like every 2 seconds

4 answers

✓ Accepted answer
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)

14 comments

0

OK, That look s good! But theres just one thing, I would like it to wait like 2 seconds between each :Destroy() Would that be possible?

0

just add a wait(2) above it :).

0

edited the answer for you

0

Umm, I tryed it, it worked but its not random! it does the same brick everytime! D:

0

Oh, you just want it leave a random brick? Okay I'll update the answer.

0

Thanks!!! Ill try it now!!! :D :D

0

Made a slight mistake, fixed it.

0

Um where do i put my model name in here?

0

Same place as before, I left it out accidently, just updated the answer for you again.

0

There is an Error at wait(2) Its expecting "Then"

0

missed that added it in. For clarification "any if condition should always be followed by a then keyword".

0

Hm So i have a tower of bricks and the one on the bottom name Bottom and on on top named top, it ALWAYS Delets the others but the bottom, so it seems like it not random :/

0

it's likely because bottom is rarely picked due to the fact that it's a random check between 0 and the total number of items in the model, if bottom is the first item (probably is) it's unlikely to get picked.

0

Hmm ok! Well Thanks :D O would upvote you but i need 1 REP :/

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.

  • Nen

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