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..

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

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..

local recurse = function (parent)
     for i, child in ipairs(parent:GetChildren()) do
        -- loops through the provided `parent`'s children
        if not (child:IsA('Model')) then
            --direct descendant is not a `Model`
            child:Destroy() -- or child.Locked = true
        else
            recurse( child ) -- runs the function again on `child `
        end
    end
end

recurse( workspace['path to parent'] ) -- call function on model
Ad

Answer this question