How would I fix my backround music player script?

I have a background music player in my game it shuffles through the music just fine but after all the songs are played the script stops playing the music here is my code:

wait(1)

while true do

local Sounds = script:GetChildren()

local RandomIndex = math.random(1,#Sounds)

local RandomSound = Sounds[RandomIndex]

RandomSound:Play()

RandomSound.Ended:Wait()

end

The music’s parent is the script in workspace btw, Thanks.

local Sounds = {0, 1, 2, 3, 4, 5, 6, -- Etc... Just add the AudioIds in here!}
local Sound = Sounds[math.random(1, #Sounds)]
local Player = Instance.new("Sound", workspace)
Player.Name = "Player"

while true do
    local Sound = Sounds[math.random(1, #Sounds)]
    Player.SoundId = Sound
    Player:Play()
    Player.Ended:Wait()
end

This should work, though I haven’t tested it. :grin:

Edit: Also, please add this script into either StarterGui, (for a client-based system), or ServerScriptService, (for a server-based system!)

2 Likes

It gives me an error in the output saying “Failed to load sound 9467287609: Unable to download sound data” How would I fix this?

Its better to load in sounds using “rbxassetid://9467287609” instead of just the id number… it doesnt cause these issues for some reason.

1 Like

I just got “Unable to assign property SoundId. Content expected, got nil” I tried to but it inside the brackets but it just gave me another error, Thanks.

to tell you the truth i cant see why your code is not working looks OK to me but iv rewritten your code maybe you will have more luck with this

local sounds = script:GetChildren()
while true do
    local sound = sounds[math.random(#sounds)]
    sound:Play()
    sound.Ended:Wait()
end
2 Likes