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

How to remove all parts with a certain name?

Asked by 9 years ago

Can I change this script to make it remove all parts named "Spikes_Simple" [[ function lock(target) pcall(function(target) target.Locked = true end, target) for _,v in pairs(target:getChildren()) do lock(v) end end lock(script.Parent) ]]

0
please put your code in the code block format TurboFusion 1821 — 9y

1 answer

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

I'm not sure whether you are trying to Lock or Remove every child with a certain name, but from your title, it seems like you want them destroyed.

You could either use a simple for loop, like so..

1for i,child in pairs(parent:GetChildren()) do
2    if child:IsA('BasePart') and child.Name == 'Name' then
3        child:Destroy -- or Child.Locked = true
4    end
5end

Alternatively, you could use recursion (which I believe is what you were aiming for), to look through every descendant of a certain item, and lock every item matching a set expectation, like so..

01local recurse = function (parent)
02     for i, child in ipairs(parent:GetChildren()) do
03        -- loops through the provided `parent`'s children
04        if not (child:IsA('Model')) then
05            --direct descendant is not a `Model`
06            child:Destroy() -- or child.Locked = true
07        else
08            recurse( child ) -- runs the function again on `child `
09        end
10    end
11end
12 
13recurse( workspace['path to parent'] ) -- call function on model
Ad

Answer this question