I created a script to play a music list. However, after playing the first song, the second one does not play. Even when the prints suggest that they are playing.
Script (I will put what the second round prints)
local Sound = game:GetService("SoundService")
local SoundChild = Sound:GetChildren()
local function GetSong()
local ran = math.random(1, #SoundChild)
return SoundChild[ran]
end
local function PlaySong()
print("Start") --Prints
local Song = GetSong()
print(Song.Name) --Prints
Song:Play()
print(Song.Playing) --Prints
print("Playing")
Song.Ended:Wait()
print("Ended")
PlaySong()
end
PlaySong()
You should check explorer for the audio printed out by the script to make sure it looks correct (sound above 0, playing set to true, maybe some other stuff).
Also, you should avoid recursion when you don’t need it. Recursion is just going to unnecessarily slow things down and cause problems in the long run. In this case, it’s infinite recursion which is even worse.
while true do
print("Start") --Prints
local Song = GetSong()
print(Song.Name) --Prints
Song:Play()
print(Song.Playing) --Prints
print("Playing")
Song.Ended:Wait()
print("Ended")
end
I also tried the following script but it does not work as well:
local Sound = game:GetService("SoundService")
local SoundChild = Sound.SongFolder:GetChildren()
local function GetSong()
local ran = math.random(1, #SoundChild)
return SoundChild[ran]
end
while true do
local Song = GetSong()
print(Song.Name)
print("Starting")
Song:Play()
print("Playing")
print(Song.Volume)
if Song.Volume ~= 0.5 then
Song.Volume = 0.5
end
print(Song.Volume)
Song.Ended:Wait()
print("Ended")
wait(2)
end
and
local Sound = game:GetService("SoundService")
local SoundIns = Sound.SoundPlayer
local SoundIds = {
[1] = 214902446,
[2] = 165065112,
[3] = 142295308,
[4] = 160442087
}
local function GetSong()
local ran = math.random(1, #SoundIds)
return SoundIds[ran]
end
while true do
local id = GetSong()
SoundIns.SoundId = "rbxassetid://"..id
SoundIns:Play()
print(id)
print(SoundIns.IsPlaying)
print(SoundIns.Volume)
SoundIns.Ended:Wait()
wait(5)
end
Try moving your sounds to some other location and not SoundService. Not entirely sure what the root problem is, but I feel it’s something got to do with SoundService. Does it matter where your sounds are located?