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) ]]
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..
1 | for 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 |
5 | 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..
01 | local 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 |
11 | end |
12 |
13 | recurse( workspace [ 'path to parent' ] ) -- call function on model |