Max Parts in a folder Script

Hello, I am trying to figure out how to script when a folder has max parts it will stop , anyone got any ideas?

2 Likes

You can get the number of instances in a folder by getting the length of its children:

local instances = #folder:GetChildren()

With # you can get the length of a table since GetChildren() returns a table.

Otherwise if there are parts and models in the folder and you only want to get the number of parts you can filter them using the IsA() function.

local partsCount = 0

for instance, index in pairs(folder:GetChildren()) do
    if instance:IsA("Part") then
        partsCount += 1 -- Add 1 to the parts count
    end
end

Heres my Example Script.

local folder = Instance.new('Folder')
folder.Name = 'folderParts'
folder.Parent = workspace

local maxparts = 40
local parts = 0

folder.ChildRemoved:Connect(function(child)
	parts = parts - 1
end)

while true do
	task.wait(.1)
	if parts < maxparts then
		parts = parts + 1
		local part = Instance.new('Part')
		part.Parent = folder
		part.Position = Vector3.new(0,20,20)
		game:GetService('Debris'):AddItem(part, 10)
	end
end

Note: Script can be inaccurate or what so, just make that script as example.

You could make it so before adding a part to the folder you check if it already has the maximum amount of parts, and if it is, you just use the return keyword

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