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

How to make a shuffle music player?

Asked by 5 years ago

I have been working on this for a while and I don't know why. This script keeps breaking after like 10 mins.

local radio = script.Parent

radio.Ended:Connect(function(shuffle)
        local song = math.random(1,20)
         if song == 1 then
            if radio.SoundId == "rbxassetid://1566658380" then
                return(shuffle)
            else
                radio.SoundId = "rbxassetid://1566658380"
            end
        elseif song == 2 then
            if radio.SoundId == "rbxassetid://2385614054" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://2385614054"
            end
        elseif song == 3 then
            if radio.SoundId == "rbxassetid://1846422780" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1846422780"
            end
        elseif song == 4 then
            if radio.SoundId == "rbxassetid://255312153" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://255312153"
            end
        elseif song == 5 then
            if radio.SoundId == "rbxassetid://320026033" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://320026033"
            end
        elseif song == 6 then
            if radio.SoundId == "rbxassetid://177272902" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://177272902"
            end
        elseif song == 7 then
            if radio.SoundId == "rbxassetid://163628975" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://163628975"
            end
        elseif song == 8 then
            if radio.SoundId == "rbxassetid://243789412" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://243789412"
            end
        elseif song == 9 then
            if radio.SoundId == "rbxassetid://336955829" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://336955829"
            end
        elseif song == 10 then
            if radio.SoundId == "rbxassetid://343057937" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://343057937"
            end
        elseif song == 11 then
            if radio.SoundId == "rbxassetid://177273163" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://177273163"
            end
        elseif song == 12 then
            if radio.SoundId == "rbxassetid://346785617" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://346785617"
            end
        elseif song == 13 then
            if radio.SoundId == "rbxassetid://1846422987" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1846422987"
            end
        elseif song == 14 then
            if radio.SoundId == "rbxassetid://1840824372" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1840824372"
            end
        elseif song == 15 then
            if radio.SoundId == "rbxassetid://1846421752" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1846421752"
            end
        elseif song == 16 then
            if radio.SoundId == "rbxassetid://1846422733" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1846422733"
            end
        elseif song == 17 then
            if radio.SoundId == "rbxassetid://378632345" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://378632345"
            end
        elseif song == 18 then
            if radio.SoundId == "rbxassetid://1047855838" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1047855838"
            end
        elseif song == 19 then
            if radio.SoundId == "rbxassetid://1345134355" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://1345134355"
            end
        elseif song == 20 then
            if radio.SoundId == "rbxassetid://289239824" then
                return(shuffle)
            else
            radio.SoundId = "rbxassetid://289239824"
            end
        end 
        radio.TimePosition = 0
        wait(2)
    radio.Playing = true
end)

2 answers

Log in to vote
1
Answered by 5 years ago

Hello there!

I am first going to help you with your script, as you have hardcoded it. I recommend that you instead just use a table to hold all the songs for you, so you don't have to do if here and there 20+ times.

A simple fix:

-- All the songs
local songs = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
-- The radio
local radio = script.Parent;
local lastPlayed;

-- Function to choose a random song
function chooseSong () 
    local randomSong = songs[math.random(1,#songs)];
    -- If it was just played
    if randomSong == lastPlayed then return chooseSong() end

    lastPlayed = randomSong;
    return randomSong;
end

-- Once the song has ended
radio.Ended:Connect ( function ()
    -- Set the new song
    radio.SoundId = chooseSong();
    -- Play the new song
    radio:Play();
end)

-- Start the radio
radio.SoundId = chooseSong();
radio:Play();

There you go, I hope you learned something new today :D If you have any questions, don't hesitate to ask!

0
Lol, we legit did the same answer at he same time decla123 45 — 5y
0
Thanks man, I did. ReallyUnikatni 68 — 5y
0
Although in line 26 it needs to be "rbxassetid://"..chooseSong(); ReallyUnikatni 68 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

One thing you can definately do to improve the script is use a table with the song IDs instead of having 20 'if' statements:

local songs = {2535,6363,728}

then have the math.random() pick a random one:

local chosen = songs[math.random(1,#songs)]

once you have that value just simply change the sound id to the following variable::

local songid = "rbxassetid://"..chosen

and change the sound's song id!

(you can have this setup in a while true do loop or something of the sort.)

Answer this question