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.
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!
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