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

How to keep a randomized playlist playing music?

Asked by 6 years ago
Edited 6 years ago

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.

0
Are you calling the Music function only once? Wafflecow321 457 — 6y
0
No, I call it each time a song is over. bluestreakejjp 41 — 6y
0
The number is from 1 to 50. Do you have 50 statements? Z_DC 104 — 6y
0
Yes I do, I only posted 3 of them because they are all the same (except the sound ids) bluestreakejjp 41 — 6y
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 bluestreakejjp 41 — 6y

2 answers

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
6 years ago
Edited 6 years ago

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!

0
You could also use a table with plenty of SoundId's and loop through that table picking one out at random. MarceloFBentley 13 — 6y
0
I had that idea, but I would've overcomplicated it. :rooDerp: Fifkee 2017 — 6y
0
Thank you so much! Everyone that contributed, I appreciate your answers. bluestreakejjp 41 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

Answer this question