How do i count up by numbered models

I’m trying to make a slicing script that slices a mesh into layers using parts, now i am trying to count up from the lowest layer to the highest layer but idk how to do that. The layers are numbered by the parts inside’s Y positions.

Layers in explorer:
image

4 Likes

Loop though the Parts model, and store them in a table.

local Layers = {}

for _, layer in Parts:GetChildren() do
	table.insert(Layers, layer.Name)
end
1 Like

Isn’t that just transferring one table to the other? Anyways ill still try that.

Tried your code by adding a print but it’s still looping trough it uncorrectly.

local Layers = {}

for _, layer in layers do
	table.insert(Layers, layer.Name)
end

for i,v in pairs(Layers) do
	print(v.Name)
end

Also the current code to loop trough it that i have now is this:

local function GetLowest(tab)
	local numtable = {}
	for i ,v in pairs(tab) do
		if v:IsA("Model") then
			table.insert(numtable,tonumber(v.Name))
		end
	end
	table.sort(numtable)
	return math.min((unpack(numtable)))
end

for i = 1,#layers do
	local layer = GetLowest(layers)
	local v = table.find(layers,tostring(layer))
	table.remove(layers,v)
	print(layer,layers)
end
1 Like
local Layers = {}

for _, layer in Parts:GetChildren() do
	table.insert(Layers, tonumber(layer.Name))
end

maybee???

1 Like

its a string so you use “tonumber()” to convert it to a number

It’s still unsorted
What i want to do is basically loop trough the model by starting at the lowest number and ending at the highest one in order.
Sorry for bad explanation in original post btw.

no problem

table.sort(Table, function(A, B)
    return A > B
end)
1 Like

replace “Table” with your table

As nils mentioned you can sort table by this function and compare the size of number.

More of expanded pseudo-code

local partsModel = <Parts model>
local partsArray = partsModel:GetChildren()

table.sort(partsArray, function(a, b)
	return a.Name < b.Name
end)

This will sort instances from smallest to biggest (by name).

2 Likes