For loop doesnt delete parts in folder, with error message

In this script I am trying to get everything inside folders, and single parts to delete. This includes unions etc. However I come up with an error whenever I try to delete something from the folder. I’ve switched up a lot of the way to try and get the parts in each folder, but each way i’ve tried has failed (I am not very good at scripting)

The error says:

attempt to iterate over a boolean value

The script is this, error where I say “for _, x in folders do”

local proxyprompt = script.Parent.ProximityPrompt

proxyprompt.Triggered:Connect(function()
	for _, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Part") then
			v:Destroy()
		elseif v:IsA("Folder") then
			local folders = v:IsA("Folder")
			for _, x in folders do
				x:Destroy()
			end		
		end
		
	end
		
end)

The issue is this bit of code

local folders = v:IsA("Folder")
			for _, x in folders do
				x:Destroy()
			end	

v:IsA(“Folder”) returns a boolean value.
instead use:

if v:IsA("Folder") then
			for _, x in x:GetChildren() do
				x:Destroy()
			end	
end

Edit: As Den_vers mentions below, you already checked if v is a folder, so you could reduce it to just

for _, x in x:GetChildren() do
		x:Destroy()
end

Edit 2: The code provided here is probably the best solution for your problem, as it’s a lot neater and provides support for accessing folders more than 1 level deep.

1 Like

You can’t directly iterate over an instance. I think you meant to do

for _, x in folders:GetChildren() do
1 Like

Like he said, this is your line. you already know it’s a folder, you don’t need to do this. Instead use v:GetChildren().

1 Like

That particular code still wouldn’t work.

local folders = v:IsA("Folder")

folders is defined as a boolean, stating whether x is a folder. Using GetChildren() on a boolean will cause another error.

1 Like

I fixed your code a bit:

local proxyprompt = script.Parent.ProximityPrompt

function proximityPromptTrigger()
	for _, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Part") then
			v:Destroy()
		end
	end
	local folders = game.Workspace:FindFirstChildWhichIsA("Folder")
	for _, v in pairs(folders:GetChildren()) do
		if v:IsA("Part") then
			v:Destroy()
		end
	end
end

proxyprompt .Triggered:Connect(proximityPromptTrigger)

what I did here was separate everything and do a cleanup, this code works fine for me

1 Like

oops i misread the code

also i think he should just be using :GetDescendants()

local proxyprompt = script.Parent.ProximityPrompt

proxyprompt.Triggered:Connect(function()
	for _, v in workspace:GetDescendants() do
		if v:IsA('BasePart') and v.Parent:IsA('Folder') then
			v:Destroy()	
		end	
	end		
end)
1 Like

By deleting an instance, you would also be deleting all of their descendents, so using :GetChildren() would still work

edit: Nvm, I reread the original post. We seem to be good at misreading things

1 Like

Welcome to the club of not reading things correctly! We’ve all done that!

Prototrodes code is v:IsA('BasePart') and v.Parent:IsA('Folder')
v:IsA(‘BasePart’) and v.parent:IsA(“folder”), so it is fine

1 Like

Saw 2 IsA’s and an ‘and’ so i just blatantly pointed it out without reading fully lol

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.