Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does my background music script only play 2 songs then stops?

Asked by 5 years ago

As said in the title, my background music script which is supposed to play a random songs after one ends is only playing 2 songs before it stops.

Script:

local musicFolder = script.Parent.Music
local currentlyPlaying = script.CurrentlyPlaying

local firstSong = musicFolder:GetChildren()[math.random(1,#musicFolder:GetChildren())]
firstSong:Play()
currentlyPlaying.Value = firstSong.Name

musicFolder:WaitForChild(currentlyPlaying.Value).Ended:Connect(function()
    local nextSong = musicFolder:GetChildren()[math.random(1,#musicFolder:GetChildren())]
    nextSong:Play()
    currentlyPlaying.Value = nextSong.Name
end)

1 answer

Log in to vote
0
Answered by 5 years ago

I think your script would benefit from a loop. My advice is use this as a template and put the sound ids in the table at the top:

local Ids = {2530779901,1050582432}
local Sound = Instance.new("Sound", game.Players.LocalPlayer:WaitForChild("PlayerGui"))

while wait() do
    local Number = math.random(1,#Ids)
    local SoundId = "rbxassetid://" .. Ids[Number]

    Sound.SoundId = SoundId
    repeat wait() until Sound.IsLoaded == true
    Sound:Play()
    wait(Sound.TimeLength)
end

This has worked for me in the past so give it a go

Ad

Answer this question