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

How to to randomize the order of songs playing in a game?

I have a button in Workspace. Inside that button is a Click Detector, and a Sound. The Click Detector has a script inside it. I'm trying to make the script play songs on shuffle when the button is not pressed to change the song. I'm having trouble scripting this part, as I'm still learning how to script :) Any help would be greatly appreciated!!

it keeps saying - Script timeout: exhausted allowed execution time

local songs = {
    "http://www.roblox.com/asset/?id=142296509",
    "http://www.roblox.com/asset/?id=154824158",
    "http://www.roblox.com/asset/?id=142677206",
	"http://www.roblox.com/asset/?id=166247544",
	"http://www.roblox.com/asset/?id=155603984",
}
local songLength = #songs
local Sound = script.Parent.Parent.Sound
local CD = script.Parent
local randomGenerator = Random.new()
local selected = 0
Sound.Pitch = 1
Sound.Volume = 3
Sound.Name = "Player"

buttonpr = false
enabled = true
while not buttonpr do
	 selected = randomGenerator:NextInteger(1, #songs)
        local chosen = (selected/#songs)
            chosen = #songs
        Sound.SoundId = songs[chosen]
        Sound:Play()

end

CD.MouseClick:Connect(function()
        if not enabled then
            return
        end
        enabled = false
        selected = randomGenerator:NextInteger(1, #songs)
        local chosen = selected % #songs
        if chosen == 0 then
            chosen = #songs
		Sound:Stop()    
		end
        print(chosen)
        Sound.SoundId = songs[chosen]
        Sound:Play()
	    Sound.Looped = true
		buttonpr = true
	        wait(30)
		buttonpr = false
        enabled = true
    end
)

5 comments

0

Wait so you want to randomize the songs played but still keep the same looping feature?

0

Why not just shuffle your table?

0

i want to randomize the order the songs play so each time you join a game its different

0

yea so do a Game.Players.PlayerAdded then connect it to a function then do the random thing in the function.

0

i want the music to play from the part the script is in, which is button, and also not interfere with how the button functions and changes the song

1 answer

local songs = { --Your song ids in this array
    34254325, --random number
    2543252, --Another random thing
}

--while true do
    local Index = math.random(1, #songs) --Generating a number from 1 to the amount of values in the table.

    local SongChose = songs[Index] --Getting the random song

    --The rest of your code
--end

Note: Make sure you call the math.random every time you want to change song so it does not play the same song non stop and annoy people.

2 comments

0

it doesnt work :(

0

Well no duh, you must implement you code to it. I just gave a sample.

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