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:
And here is The order that they are ordered in:
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
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
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,
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!
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.