Delete children of folder

Purpose
A building game, where building blocks (through StarterCharacterScripts.Localscript) are added by players to a folder in workspace (game.Workspace.BlocksFolder). Once the loop (round) is over, the building blocks should be cleared from the folder.

Issue
The problem is that the children of the folder won’t be cleared once it’s called within the loop.


game.ServerScriptService.RoundScript:

-- rest of code

for _, Block in pairs(game.Workspace.BlocksFolder:GetChildren()) do
			if Block and Block:IsA("BasePart") and Block.Name == "Block" then
				game.Workspace.BlocksFolder:ClearAllChildren()
				-- prints
				print("Children left:" .. game.Workspace.BlocksFolder)
				print(game.Workspace.BlocksFolder:GetChildren())
			end
		end

-- rest of code
	end
	
end
  • I am left with no error or any of the prints in the output (and have tried a variety of ways to try solving it)

  • I tried printing the children of the folder when playtesting, which returned a table with all of the blocks

  • Once I tried for _, Block in pairs(game.Workspace:GetChildren()) do if Block and Block:IsA("BasePart") and Block.Name == "Block" then print("blocks destroyed") end end the output did leave me with “blocks destroyed (x32)” meaning that the 32 blocks were detected, but not destroyed


How do I solve this?
Thanks.

2 Likes

Can’t you just do Folder:ClearAllChildren() outside of a for loop?

workspace.Folder:ClearAllChildren()

But if you want to do it in a for loop you can just do:

-- Alternate method
for _, instance in Folder:GetChildren() do
	if instance:IsA("BasePart") then
		instance:Destroy()
	end
end
2 Likes

Tried your solutions:

  1. game.Workspace.BlocksFolder:ClearAllChildren() didn’t work outside of the loop, only when I ran it in playtesting mode through the cmd bar

  2. I suppose the 2nd method didn’t differ from the one I was using (fewer criteria before destroying the instances). Sadly, it didn’t work either and I’m left with no errors or anything.

2 Likes

Using BlockFolder:ClearAllChildren() will result in ALL of the children getting cleared from the folder. In your original script, you check to ensure that the block is a BasePart and that the block’s name is “Block”. Using BlockFolder:ClearAllChildren() in your for loop is redundant, as it would clear ALL of the children of the folder on the first occurance.

@Amritss’s 2nd method should work. I am not sure why it didn’t for you.

So, here is my question for you:

Do you want ALL of the children in the folder to be deleted or only certain children (such as any child named “Block”)?

If you want to get rid of ALL the children, just call BlockFolder:ClearAllChildren() and delete your loop.

If you want to test for certain children, keep your for loop (which already should work) and actually destroy the part.

for _, Block in pairs(game.Workspace.BlocksFolder:GetChildren()) do
	if Block and Block:IsA("BasePart") and Block.Name == "Block" then
		Block:Destroy()

		-- prints
		print("Children left:" .. game.Workspace.BlocksFolder)
		print(game.Workspace.BlocksFolder:GetChildren())
	end
end

Hope this helps!

1 Like

why are you deleting everything in the first iteration?
just do this:Folder:ClearAllChildren()

or if you want to keep your if stmt:

for _, block in ipairs(folder:GetChildren()) do
--useless to say if block because things passed to a for loop aren't nil, 
--unless modified outside of the loop
  if not block:IsA("BasePart") or block.Name ~= "Block" then continue end
  block:Destroy()
 
 print("Children left:" .. #folder:GetChildren())
--print(folder:GetChildren()) will print a table id, which is useless in debugging.
end

if it doesn’t work, then maybe the rest of the code is causing it.

1 Like

I thought you wanted to clear all the children, my bad. But yeah the second method should’ve worked like @Fittergem1000 said. Perhaps your script is disabled? Or maybe your studio is cursed. OR maybe a script is overriding this script.

1 Like


-- Delete all blocks in the BlocksFolder
game.Workspace.BlocksFolder:ClearAllChildren()


1 Like

Just for reference, it only prints a table id if you concatenate it. If you printed the table on it’s own, it’ll print the contents of the table.

1 Like