✓ Accepted answer
Firstly, let's use an infinite loop (while true do ... end) to have the music system function indefinitely:
while true do
end
Now, we'll pick and play a random song at the beginning of the statement body. Instead of using math.random, we'll use the recommended Random object; the algorithm provides more randomised numbers. According to some developers, math.random has been updated to use this new algorithm anyway, but I cannot confirm this. We'll also refrain from using the second parent parameter of Instance.new, as it is deprecated:
local songs = {
160442087,
705243700,
}
local sound
local random = Random.new()
while true do
sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://" .. songs[random:NextInteger(1, #songs)]
sound.Parent = workspace
sound:Play()
end
Lastly, let's use the Sound.Ended event to wait until the song ends. Once it ends, we'll start again:
local songs = {
160442087,
705243700,
}
local sound
local random = Random.new()
while true do
sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://" .. songs[random:NextInteger(1, #songs)]
sound.Parent = workspace
sound:Play()
sound.Ended:Wait()
end
2 comments
How many songs do you have?
Try the new script my edited script.