Index order changing every time I playtest

I’m trying to index a folder filled with 8 parts yet every time I test it out a different index order gets outputted

image
image
image

I don’t know what to do so the only script I’m using is

local seeds = ReplicatedStorage.Seeds:GetChildren()
print(seeds)

I tried using for loops with and without ipairs but they give the same random result each time.

table.insert can fix this problem (I think it works)

Not sure if it will work but, you can try moving seeds to folder one by one

It may be because you are doing this on the client and things are replicated to the client. So it has a random order. Kinda hard to explain. Try the same code on the server.

1 Like

Another thing you can do is to write script that gets all seeds one by one in table
like it searches for seed1 then inserts it in and breaks when found

(I dont think you actually have to break loop)
You can do this with for loop

1 Like

randomized on the server as well
image

Then you can do this with for loop

like

for i=1, #ReplicatedStorage.Seeds:GetChildren() do

end

what exactly do i do with this?

nevermind, they arent randomized but still not in the position id want them to be, everytime i playtest i get this result
image
sane as the one above

shouldn’t a table.insert with an ipairs loop do the trick?

2 Likes
for i=1, #ReplicatedStorage.Seeds:GetChildren() do
     local seedname = "Seed" .. i
     if ReplicatedStorage.Seeds:FindFirstChild(seedname) then
           -- do thing
     end
end
1 Like

Why do you need the folder to be ordered? Is it to get an ordered table?

Here’s two things you could do:

  1. Get the children, and then sort them if you only need this to be sorted for a script.
local children = folder:GetChildren()
table.sort(children, function(aChild, bChild)
    local aNum = string.match(aChild.Name, "%d")
    local bNum = string.match(bChild.Name, "%d")

    return aNum > bNum
end)
  1. This is basically suggestion 1 but re-inserting them into the folder
local rStorage = game:GetService("ReplicatedStorage")
local temp = Instance.new("Folder", rStorage)
temp.Name = "Temp"

--get children
local children = rStorage.SomeFolder:GetChildren()

--parent to temp
for _, child in next, children, nil do
    child.Parent = temp
end

--sort the children
table.sort(children, function(childA, childB)
    local aNum = string.match(childA.Name, "%d")
    local bNum = string.match(childB.Name, "%d")

    return aNum > bNum
end)

--re-parent children
for _, child in next, children, nil do
    child.Parent = rStorage.SomeFolder
end

Roblox sorts children alphabetically, so it may be that you need to manually sort and re-parent.

2 Likes

Maybe you need to manually change the order of the parents. I think anyway.

1 Like

yeah i think that was the problem, i created a new folder and just added all my seed parts in there and works as i intended
i think this was because i duplicated some parts in the folder itself

1 Like

Didn’t knew about table.sort
Seems to be useful

2 Likes

The :GetChildren() method returns an unsorted array of the parents children, you can sort this array using the table.sort() function

local Folder = script.Parent
local Children = Folder:GetChildren()

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

for _, Child in ipairs(Children) do
    print(Child.Name)
end

Edit: I don’t know why but I didn’t see 12345koips reply, nevermind then