I think you didnt understand me. I dont want to move them anywhere. Just check if Objects in Folder (in above example) are sorted so object1 exist, object2 exist and so on
Oh ok, I believe that you get the exact order if you just get the children with Instance:GetChildren(). Then you can evaluate the resulting table (see @nicemike40’s answer).
local obj = {}
local folder = workspace.Folder
local children = folder:GetChildren()
for i = 1, #children do
local found = folder:FindFirstChild("object" .. i)
if not found then
-- either there's an extra object or we're missing a num
error(string.format("could not find object%d", i))
end
obj[i] = found
end
print("list of objects:")
print i, v in ipairs(obj) do
print(i, v)
end
I would caution against being this strict though. I would maybe just throw a warning and stop the loop after the first name that doesn’t match.
--[[
"root" argument is known as "Folder" in my example while "namePattern" will be "object".
]]--
function sortObjects(root, namePattern)
local children = root:GetChildren()
local count = #children
local sorted = {}
for current = 1, count, 1 do
local found = nil
for _, child in pairs(children) do
local s, number = pcall(function()
local newName = string.gsub(child.Name, namePattern, "")
return tonumber(newName)
end)
if not s or number == nil then
error("Name does not fit pattern!")
end
if number == current then
found = child
end
end
if found ~= nil then
sorted[current] = found
else
error("Objects are not sorted properly!")
end
end
return sorted
end