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

How to remove all parts with a certain name?

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) ]]

1 comment

0

please put your code in the code block format

1 answer

✓ Accepted answer

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

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