Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by
Bwc904 0
10 years ago

Please include the code which you are trying to use, so the community will be better-equipped to help you with your problem.

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

0
Also i dont want it to delete them all at once like every 2 seconds Bwc904 0 — 10y

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?

4 answers

Log in to vote
0
Answered by
Andalf 100
10 years ago
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)
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? Bwc904 0 — 10y
0
just add a wait(2) above it :). Andalf 100 — 10y
0
edited the answer for you Andalf 100 — 10y
0
Umm, I tryed it, it worked but its not random! it does the same brick everytime! D: Bwc904 0 — 10y
View all comments (10 more)
0
Oh, you just want it leave a random brick? Okay I'll update the answer. Andalf 100 — 10y
0
Thanks!!! Ill try it now!!! :D :D Bwc904 0 — 10y
0
Made a slight mistake, fixed it. Andalf 100 — 10y
0
Um where do i put my model name in here? Bwc904 0 — 10y
0
Same place as before, I left it out accidently, just updated the answer for you again. Andalf 100 — 10y
0
There is an Error at wait(2) Its expecting "Then" Bwc904 0 — 10y
0
missed that added it in. For clarification "any if condition should always be followed by a then keyword". Andalf 100 — 10y
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 :/ Bwc904 0 — 10y
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. Andalf 100 — 10y
0
Hmm ok! Well Thanks :D O would upvote you but i need 1 REP :/ Bwc904 0 — 10y
Ad
Log in to vote
0
Answered by
war8989 35
10 years ago

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
Log in to vote
0
Answered by
JayByte 25
10 years ago

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)

Log in to vote
0
Answered by 10 years ago

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