Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to keep a randomized playlist playing music?

Hello. I made a music playlist. It plays one random song, then another song doesn't play.

Here is part of the script I have.

function Music()
local rand = math.random(1,50)
	if rand == 1 then
	script.Sound.SoundId = 'rbxassetid://755156652'
	script.Sound:Play()
	wait(188)
	Music()
	end
if rand == 2 then
		script.Sound.SoundId = 'rbxassetid://186783853'
	    script.Sound:Play()
	wait(120)
	Music()
	end
	if rand == 3 then
		script.Sound.SoundId = 'rbxassetid://685388224'
	script.Sound:Play()
	wait(120)
	Music()
	end

The full script goes on for the different 50 'rands'. Is it the function that is wrong?

Help would be appreciated.

5 comments

0

Are you calling the Music function only once?

0

No, I call it each time a song is over.

0

The number is from 1 to 50. Do you have 50 statements?

0

Yes I do, I only posted 3 of them because they are all the same (except the sound ids)

0

I was wrong in my problem, it doesn't play any music whatsoever. The output says 'Sound Workspace.Script.Sound Failed to load rbxassetid://0'. So it has to be a problem with the script

2 answers

✓ Accepted answer

How are you doing, bluestreakejjp!

I would store all the sounds with their names ranged from 1 through whatever the amount of songs you want, inside the script. It doesn't matter!

script[math.random(1, #script:GetChildren())]:Play()

for i, v in pairs(script:GetChildren()) do
	if v:IsA("Sound") then
		v.Ended:connect(function(PlayNextSound)
			local getSound = script[math.random(1, #script:GetChildren())]
			if v:IsA("Sound") then -- IF it is a sound
				getSound:Play()
			else
				repeat wait() -- if it is NOT a sound, then it will repeat it until it is a sound
					getSound = script[math.random(1, #script:GetChildren())]
				until getSound.ClassName == "Sound"
				getSound:Play()
			end
		end)
	end
end

Remember! Think smarter, not harder! If you're not sure how to do something, make sure to tag by here and ask, or you can go to the Roblox Wiki! It seriously has helped me out so many times.

If you accidentally skip a number, you can add an if statement to check if a FindFirstChild is valid.

I hope this helped you with your question, and if you need more information, don't be afraid to ask!

3 comments

0

You could also use a table with plenty of SoundId's and loop through that table picking one out at random.

0

I had that idea, but I would've overcomplicated it. :rooDerp:

0

Thank you so much! Everyone that contributed, I appreciate your answers.

This is quite a simple script; it may not work - if it doesn't, I'll write up a better answer. Infact, I'll rewrite the whole script for you as you have shown you've tried.

Sorry about the ugliness of the script.

function Music()
local rand = math.random(1,3) -- you only have 3 songs
    if rand == 1 then
    script.Sound.SoundId = 'rbxassetid://755156652'
    script.Sound:Play()
    wait(188)
    script.Sound:Stop()
    end
if rand == 2 then
       script.Sound.SoundId = 'rbxassetid://186783853'
       script.Sound:Play()
    wait(120)
	 script.Sound:Stop()
    end
    if rand == 3 then
        script.Sound.SoundId = 'rbxassetid://685388224'
    script.Sound:Play()
    wait(120)
 script.Sound:Stop()
    end

while true do
Music()
wait(0.5)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.