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

Button to destroy all children with one name not working?

I've made a destroy button, that when clicked, destroyes all of something called "Bread". Doesn't seem to work :/

local cd = game.Workspace["ClearBread Button"].Button.BreadRemover
local found = workspace:FindFirstChild("Bread")

script.Parent.MouseClick:connect(function()
	while true do
	if found then
		found:Destroy() --destroy everything called "Bread"
		wait(1)
		cd.Parent.BrickColor = BrickColor.new("Really red") -- make the button red
		cd.Parent = game.Workspace --unparents it so it can't be clicked again
		wait(20) --cooldown time
		cd.Parent = game.Workspace["ClearBread Button"].Button --reparents it
		cd.Parent.BrickColor = BrickColor.new("Forest green") --changes button back
		wait(.1)
	end
end)

The explorer looks a little like this:

ClearBread Button --Model
	Button --Part
		BreadRemover --ClickDetector
			Script --The script I just posted

Please help me out if you can, I'll accept your answer if it works or you find an alternative way to make it work C:

(In case you need to know, I only want the Bread in Workspace to be destroyed, I have another "Bread" in ReplicatedStorage which I do NOT want to be Destroyed)

2 answers

This method will only destroy one of the children named bread.I would recommend putting all the bread in a folder named bread and deleting the folder instead.

stardon's answer is your best bet as it is the most efficient by far however if for some reason you are determined to do this through searching the children then you can use GetChildren instead and sort through them to find "Bread"

for _, child in pairs(workspace:GetChildren()) do
	if child.Name == "Bread" then
		child:Destroy()
	end
end

Just incase you discover that Bread is inside of something else and not a child of workspace directly you can always replace the first line with

for _, child in pairs(workspace:GetDescendants()) do

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