My ipairs Loop is not working correctly

So I Made a Folder Containing Some Sounds
And I Wanted To Loop Through Every Sound in That Folder and But them in a Table

The Problem is That they don’t Get inserted into the Table in Correct Order

Here is A Code Sample

local Music_Folder = game:GetService("ReplicatedStorage"):WaitForChild("Music")
local Sounds = {}

for i, v in ipairs(Music_Folder:GetChildren()) do
	table.insert(Sounds, v)
end

Here is The Order that they are supposed to be ordered in:
Annotation 2023-08-03 200555

And here is The order that they are ordered in:
Annotation 2023-08-03 200859

as you can see, they aren’t in The Same order (even though i used ipairs instead of the normal pairs)

now, i Have Tryed A lot of solutions like,

Inserting The Sound Name Instead of the entire Object,
using pairs instead of ipairs
changing the order that they need to be in,
Changing the Folder’s Location.

and none of them Worked

Any Help on This Sittuation Will be Very Very Appreciated

5 Likes

:GetChildren() doesnt get them in the same order like the one in the explorer.

1 Like

Does it also happen if You try doing the same thing but on server? I have an idea how to fix it but You need to test it first.

1 Like

I’ve Noticed That
I tryed encountring This By Adding a Wait(10)
Before the ipairs Loop
and it also didn’t Work

1 Like

i did, using the same ipairs Loop and everything
and Still The Same Problem

2 Likes

Well it’s never gonna be the same order, so ipairs isn’t gonna help or a wait.

1 Like

I can only think of making a table with strings representing songs names in the specific order and using that to make an another table with sounds.

1 Like

I see what you are trying to do. You are trying to sort them by alphabetical order! Correct?

If so, use table.sort. Here is an example I found on Devforums by EmbatTheHybrid for alphabetical sort:

local tbl = {"B", "a", "g", "e"}

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

for i,v in pairs(tbl) do
    print(v) 
end

It will give you this:

B
a
e
g

Here is the post for further information on this topic.

I Already Thought About This, The Only Problem is That if i wanted to add like 10 other songs, i Would Need To Type Their Names In The Table, Wich is not very ideal

1 Like

The Thing is That I Have another ipairs Loop That Does the Same Thing, But Instead of adding the sounds to a table, it Creates Buttons for Them, and that loop is in Perfect Sync With the order of the Sounds in the Music Folder
and it doesn’t Have anything fancy to it.

local Button_Template = script.Parent.Template_Button

wait(10)
for i, v in ipairs(Music_Folder:GetChildren())
      Local C = Button_Template:Clone()
      C.Name = v.Name.."_Button"
      C.TextLabel.Text = v.Name
      C.Parent = Button_Template.Parent
end

and when i run, The Button Gets Cloned in the Correct order after 10 Seconds

I Actually Though About using table.sort() But there was a Problem

i wanted my system to be Fluid is Possible, And as you have Noticed, The Songs Have Very Different Names and Values, Plus There is no table sorting Algorithm that Could help me here,

But thanks for your effort

Then how can i Get them in the same order as Explorer?

This may be tedious, but it’s a solution:

Name all the sounds 1 2 3 or 4 and so on based on the order you want, with 1 being the first.

Use this script:

local Music_Folder = game:GetService("ReplicatedStorage"):WaitForChild("Music")
local Sounds = {}

for i, 1 = #Music_Folder:GetChildren() do
	table.insert(Sounds, Music_Folder:GetChildren()[i])
end

This script loops for the same amount of songs there are, then looks for the song named “1”, then “2”, and so on adding them into the table. It should add them in the right order!

I Also Though About That, But the only Problem is That I Will Need The Names of The Sounds

Have you tried testing it to see if you get close results?

Using a shared dictionary in a modulescript and number-based names you can use this.

local Music_Folder = game:GetService("ReplicatedStorage"):WaitForChild("Music")
local Sounds = {}

for i, 1 = #Music_Folder:GetChildren() do
	Sounds[tostring(Music_Folder:GetChildren()[i]):sub(2)] = Music_Folder:GetChildren()[i]
   --[[Optional renaming once done
      Music_Folder:GetChildren()[i].Name = tostring(Music_Folder:GetChildren()[i]):sub(2)
   --]]
end

You can also use this method to rename the sounds after you’re done with the loop.
We can remove the number (first-letter) in the sound name easily to find it.
like this
Sounds.Test:Play()

The person above me made a better system, but just because, another tedious thing, you could manually set the table:

Sounds = {
sound1 = --sound number 1
sound2 = --sound number 2
sound3 = --sound number 3
sound4 = --sound number 4}

But unfortunately, looking at how many sounds you have that could take a while. If you’re up to it, you could try it.
If you won’t, maybe just try and remember the numbers. You said you need the names, probably for accessing it from another script, so maybe you could just remember by thinking this:
1 = Rain Sounds
2 = Other Music

Other than that, maybe you could add an IntegerValue under each song with the order, then use this script:

local Music_Folder = game:GetService("ReplicatedStorage"):WaitForChild("Music")
local Sounds = {}
local songsToAdd = Music_Folder:GetChildren()
local currentNumber = 1
local name = --what you name the integer values

repeat
	for i,v in ipairs(Music_Folder:GetChildren()) do
		if v:FindFirstChild(name) then
			if v[name].Value == currentNumber then
				table.insert(Sounds,v)
				currentNumber += 1
				table.remove(songsToAdd,table.find(songsToAdd,v,1))
			end
		end
	end
until #songsToAdd <= 0

Note that this last script may not work; I didn’t test it but hopefully it goes right.

Tried And it made The Sorting even Worse

i Also Though About This, But i got The Same Exact Problem, The Loop is Going in Wrong Order
Because of That, The Values Gets Set in Wrong Order.

look @sir_gagigo, @weakroblox35 and @bt6k, how about you guys try testing the original Code that i posted Above and see if the Order is Correct or not

Because i don’t want to tire you guy more than that.