Sorting Parts based on triangle amount - Roblox Studio Scripting

Hi, I want to create a script that when run gets all the parts in a folder and then makes a bunch of new folders and sorts the parts into each folder, once a folder is full (has under 10,000 triangles) then make a new folder and fill it up and loop until there are no parts left to sort and all folders are full. Btw, if it helps each folder has parts and wedges, nothing else, so if there is a way to find out how many triangles are in each folder based on that.
If you can help that’d save my game.
Cheers,

1 Like

So when you say triangles, you are talking about the poly/vertices count for the mesh that makes up that part, correct?

If that’s the case, a cube/rectangle makes up 12 triangles (2 for each side/face) and wedges make up 8 triangles. 2 for each side/face unless the face is already a triangle, which there is 2.

You can then count your parts and increment by 12 for each part and increment by 8 for each wedge. when that number reaches 10,000 move the parts counted and make a new folder and reset the counter.

local function moveParts()
	local n = 0
	local partsToMove = {}
	for k,v in pairs(objects:GetChildren()) do
		if v:IsA("WedgePart") then
			n += 8
		else
			n += 12
		end
		if n <= 10000 then
			table.insert(partsToMove, v)
		else
			break
		end
	end
	
	-- TODO: make new folder here and put parts inside partsToMove into that folder.
	
	if #objects:GetChildren() > 0 then
		moveParts()
	end
end
2 Likes

Awesome, getting somewhere, are you able to whip up a script for me?

Your awesome. Thankyou so much!

1 Like

Well I guess this got answered while I was typing this out, but I tried my best to explain everything in detail here so it might be worth checking out. Hope you can find this helpful.


local folderSizeLimit = 3

function sortParts()
	local folderList = {} --This will be a table containing all our folders with the sorted parts
	local curFolderSize = 0 --The size of the folder we are currently filling
	local newFolder = Instance.new("Folder") --Create the first folder to start with
	
	--First we iterate through all the descendants in the part folder
	for _, part in pairs(partFolder:GetDescendants()) do
		if part:IsA('BasePart') then --Check if the descendant is a BasePart (part, wedge, etc)
			
			--if the size of the current folder is less than the limit, then we add a part to that folder
			if curFolderSize < folderSizeLimit then
				part.Parent = newFolder
				curFolderSize = curFolderSize + 1 --we also make sure to increase the current folder size variable
			else --if the size of the current folder is at the limit,
				table.insert(folderList, newFolder) --we add the current folder to our folderList
				newFolder = Instance.new("Folder") --we make a new folder to fill
				part.Parent = newFolder --and we make sure to parent the currently iterated part to the new folder, as well as set the current folder size back to 1
				curFolderSize = 1 
			end
		end
	end	
	return folderList --returns the table with our folders
end```
2 Likes

:open_mouth: Wow! Thanks for helping too! Have a great day!

I should mention, this doesn’t do exactly what the script posted above me is doing, this code sorts the parts based on the number of parts/wedges and not triangles. Though you could still use the same code while applying the principles @Dev_Ryan mentioned.

1 Like