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