All My Music Is Playing At Once. AHHHH!

All my music is playing at once. Details below.

I have a folder called music in the workspace:
image

This is my script:

while true do
	print("Choosing new song.")
	local sound = script.Parent.Sounds:GetChildren()[math.random(1,#script.Parent.Sounds:GetChildren())]
	print("Song chosen is "..sound.Name..".")
	print(sound.TimeLength)
	sound:Play()
	wait(sound.TimeLength)
end

--Hopefully you can help. Thanks!

Is the setting “Playing” on in the sounds?

No, I checked that. They are all not set to playing.

might be worth putting

sound:Stop()

at the bottom of the loop in case the sounds are repeating

But they all start playing as soon as the server is created. So that can’t be the issue.

I have to go but if anyone has any ideas feel free to share.

Alright I edited your script a bit and it seems to work:

while true do

print("Choosing new song.")

local songs = script.Parent.Sounds:GetChildren()

local sound = songs[math.random(1,#songs)]

print("Song chosen is "..sound.Name..".")

sound:Play()

sound.Ended:Wait()

end
1 Like
 if not sound.IsLoaded then sound.Loaded:wait() end

Try adding that line before it plays. It might be looping before having time to load the tracks

1 Like

Instead of what you’re doing I suggest doing the following:

while true do
	print("Choosing new song.")
	local sound = script.Parent.Sounds:GetChildren()[math.random(1,#script.Parent.Sounds:GetChildren())]
	print("Song chosen is "..sound.Name..".")
	sound:Play()
	Sound.Ended:Wait(TimeBetweenSongs)
end

Note, the sounds can’t be looped else it’ll probaly glitch.

Thought I suggest that you’d have only 1 sound instance and doing the following:

local TimeBetweenSongs = 1 -- Extra time we'll wait before playing the next song.
local Playlist = { -- Music Ids of the songs we'll play
    123456,
    1234567,
    123457,
    654321, 
-- If you want to add more sound ids then put the id here and end it with a comma.
}
local Sound = script.Parent.Audio -- Path to the audio instance

local function nextSong(id) -- Every time "nextSong()" is called this will fire. 
    -- If the parameter "id" is invalid this will break.
    Sound.SoundId = id
    Sound:Play()
    Sound.Ended:Wait(TimeBetweenSongs)
end

while true do 
    -- Anything inside of this will repeat for ever.
    nextSong(Playlist[math.random(1,#Playlist])
end
-- Anything after this will never have the chance to fire.

Took this script from a post I made before, but I edited it.

If you wish to not have the same song playing as before then I suggest doing the following:

local LastSong

local function nextSong(id) -- Every time "nextSong()" is called this will fire. 
    -- If the parameter "id" is invalid this will break.
    Sound.SoundId = id
    Sound:Play()
    LastSong = id
    Sound.Ended:Wait(TimeBetweenSongs)
end

while true do 
    -- Anything inside of this will repeat for ever.
    local CurrentSong
    repeat 
        CurrentSong = Playlist[math.random(1,#Playlist]
        wait()
    until CurrentSong ~= LastSong
    nextSong(Playlist[math.random(1,#Playlist])
end
-- Anything after this will never have the chance to fire.
3 Likes

Thanks all. I will try all of these tomorrow (as it is getting late) and tell you which of them work. Once again, thanks for helping!

@ZombieCrunchUK Yes, the problem was the fact that the script was looping even before it could load. Thanks. I just put a wait(5) before the loop. Thanks for helping!

2 Likes