Random Music Script Overlapping songs

Hi all,
I currently have a randomised music script in my game, it looks like this:

Numberofsoundz = 10

math.randomseed(tick())
for _ = 1, math.huge do
Soundisplaying = (math.random(Numberofsoundz))
if Soundisplaying == 1 then
script.S1:Play()
wait(175)
elseif Soundisplaying == 2 then
script.S2:Play()
wait(278)
elseif Soundisplaying == 3 then
script.S3:Play()
wait(129)
elseif Soundisplaying == 4 then
script.S4:Play()
wait(65)
elseif Soundisplaying == 5 then
script.S5:Play()
wait(125)
elseif Soundisplaying == 6 then
script.S6:Play()
wait(192)
elseif Soundisplaying == 7 then
script.S7:Play()
wait(117)
elseif Soundisplaying == 8 then
script.S8:Play()
wait(242)
elseif Soundisplaying == 9 then
script.S9:Play()
wait(120)
elseif Soundisplaying == 10 then
script.S10:Play()
wait(130)
end
end

Each sound is inside the script, and it randomly picks from one of them. I have got the wait times from the properties of the sound (I have 10 sounds inside the script as there are 10 used, but i’d add another sound part inside the script and add another elseif into the script). I know how to do all that. However, it takes a long time to keep adding sounds into it, and for my mute button i have to make it mute every single sound, even if its not playing. So if i have 100 different songs, i have to do the section for each song 100 times in the button. Also, some songs start before the other one has finished, but i have no way of knowing which. I know theres probably a better way than what im doing, thanks for any help!

Not sure why you are using a numeric for loop to infinity when you can just use a much clearer infinite while loop:

while true do

end

Now, no need for so many if statements. Since the song names are Sn where n is some arbitrary number, you can access the song through script["S" .. Soundisplaying].

Also, no need to hardcode wait the length of song. Use sound.Ended instead!

local pseudo_rng = Random.new(tick()) -- use the Random class instead, much better 
interface
while true do
    local random_song = script["S" .. pseudo_rng:NextInteger(1, #script:GetChildren())]
    random_song:Play()
    random_song.Ended:Wait()
end

Also I used #script:GetChildren() because again, you were hardcoding the amount of children in the script. So you would constantly need to update the variable everytime you add or remove songs.

So would I replace the entire script with just that, and what about the mute button? And would I still have each music as a child of the script (So if i had 100 songs, id put 100 music parts as a child of the script)?

Mute should stop/pause the sound, no? In that case you should do random_song.Stopped:Wait() for the former, otherwise random_song.Paused:Wait() for the latter. But this brings a second issue. What if the user doesn’t stop the song themselves.

Ok, just implemented into my game and seems to work well, much easier than before to add more songs too! The mute button still works, so all good there. Thanks so much for the help!