Detect if a folder is empty

Hello! I’m trying to make a script that when a folder is empty, something happens. I’ve tried this script but it doesn’t worked:

if #game.Workspace.MissionsStuff.Paplmito:GetChildren() == 0 then
game.Workspace.Coisinha:Destroy()

end

This is the script to make the folder empty. (He weren’t empty at first)

function onClicked()

game.Workspace.MissionsStuff.Paplmito.Script:Destroy()

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
1 Like
folder.ChildRemoved:Connect(function()
   if #folder:GetChildren() == 0 then
    -- folder is empty
   end
end)
3 Likes
local Folder = PathToFolder

Folder.ChildAdded:Connect(function()
    if #Folder:GetChildren() == 0 then
        
    end
end)

Folder.ChildRemoved:Connect(function()
    if #Folder:GetChildren() == 0 then
        
    end
end)

this should run if a child is added or removed from the folder, if you want to easily do this to any folder with less code then do this

local Folder1 = PathToFolder1
local Folder2 = PathToFolder2

function CheckFolder(Folder)
    Folder.ChildAdded:Connect(function()
        if #Folder:GetChildren() == 0 then
        
        end
    end)

    Folder.ChildRemoved:Connect(function()
        if #Folder:GetChildren() == 0 then
        
        end
    end)
end

CheckFolder(Folder1)
CheckFolder(Folder2)

either way don’t do the second option if you only need to do one folder

also if you want to do the second option just use a module script to make it look cleaner

3 Likes

The usual thing is it is based on the line code.

Like this:

-- Folder Path
local Folder = game:GetService("Workspace"):FindFirstChild("Folder")
-- Inside The Folder Which Is The Parts.
local PartInside = Folder.PartInside

-- Remove The PartInside Before We Begin To Check.
wait(1)
Folder:ClearAllChildren()
-- Or You Can Do Specific But Does Not Remove All Parts.
PartInside:Destroy()

if #Folder:GetChildren() == 0 then
    -- Code Here

    -- Example Code:
    Instance.new("Part").Parent = Folder
    -- We just added a new part to the Folder.
end

2 years later, lol

thanks for the reply btw

1 Like