Sort Folder In Workspace For Scripting Purposes

I have a tile based game and I need to organize the children of a folder for a scripting table. Simplified: I have a folder full of models labeled “BeachPathNUMBER” such as “BeachPath15”.
Temp

How would I organize this so the number progressively get larger? Or, how do I organize the table while scripting.

Thanks in advance!

Just simply do

BeachPath0001
BeachPath0002
BeachPath0003
BeachPath0004
BeachPath0010
BeachPath0100

And so on to organize the models so the number gets larger.

The question is, how do I organize the already named models. Simply renaming them won’t change their order

This should work, paste it in the command bar:

for i,v in pairs(workspace.NormalPatchAlongBeach:GetChildren()) do
    local number = v.Name:gsub('BeachPath', '')
    local str = '0000'..number
    str = str:sub(str:len() - 3, str:len())
    v.Name = 'BeachPath'..str
end
1 Like

Kinda off topic but my command bar hasn’t been working lately, when I press enter it just selects everything. Any idea how to fix this?

Method 1

local children = NormalPathAlongBeach:GetChildren();

for i = 1, #children do
  local path = NormalPathAlongBeach:FindFirstChild("BeachPath" .. i);
end;

Method 2

local children = NormalPathAlongBeach:GetChildren();

print(children); --Unsorted
table.sort(children, function(one,two)
  return tonumber(string.sub(one.Name, 10)) < tonumber(string.sub(two.Name, 10));
end);
print(children); --Sorted
1 Like

Thank you, help appreciated (as promised:)!

1 Like