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

How to randomize songs playing while in 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!!

The mouseclick function works perfectly and changes the song every 30 seconds when the button is pressed. The problem is the song shuffle part of the script. I'm trying to make it randomize the songs, as well as play them in random order until the button is pressed to change it.

local songs = {
    "http://www.roblox.com/asset/?id=2206587324",
    "http://www.roblox.com/asset/?id=4737555180",
    "http://www.roblox.com/asset/?id=2231782710",
	"http://www.roblox.com/asset/?id=2538961789",
	"http://www.roblox.com/asset/?id=2538961789"
}
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"  
--------------------   
----random songs play while button is not pressed
----song shuffle part of the script
---- not working 
local song1 = "http://www.roblox.com/asset/?id=2206587324"
local song2 = "http://www.roblox.com/asset/?id=4737555180"
local song3 = "http://www.roblox.com/asset/?id=2538961789"
local song4 = "http://www.roblox.com/asset/?id=2538961789"
local song5 = "http://www.roblox.com/asset/?id=2538961789"
 while (true) do
local track = (math.random(song1, song2, song3, song4, song5))
Sound:Play(track)
end
-------------------------
-------change song using button function
----this part works
enabled = true              
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
	    end 
	print(chosen)
    Sound.SoundId = songs[chosen]       
	Sound:Play()
	Sound.Looped = true
	wait(30)
	enabled = true
    end)

2 answers

I haven't tested this yet, but please try the following!

local Sound = script.Parent.Parent:WaitForChild("Sound")

local function tabLength(table) -- this finds the length of any table
	local count = 0
	for i in table do
		count += 1
	end
	return count
end

local songs = { -- your list of songs from before
    "http://www.roblox.com/asset/?id=2206587324",
    "http://www.roblox.com/asset/?id=4737555180",
    "http://www.roblox.com/asset/?id=2231782710",
    "http://www.roblox.com/asset/?id=2538961789",
    "http://www.roblox.com/asset/?id=2538961789"
}

local queue = {} -- a new table that we'll use to store the songs in a randomised order

local function makeQueue()
	queue = {}
	local counter = tabLength(songs)
	while counter > 0 do
		local selected = songs[math.random(1, tabLength(songs)]
		if not queue[selected] then -- make sure the song isn't already in the queue
			table.insert(queue, selected)
			counter -= 1 -- decrement the counter every time we add a song that hasn't been added
		end
	end
end

while true do -- finally, play the queues in a loop
	makeQueue() -- make a new queue after every queue has been finished
	local songNum= 1 -- so that the loop works with the skip button
	while songNum <= tabLength(queue) do
		Sound.SoundId = queue[songNum] -- replace soundId of Sound, so we don't need to make multiple sounds
		Sound:Play()
		wait() -- this should make the loop wait for the song to finish before playing the next one
	end
end

-- Now lets make the skip button
local debounce = false
local playing -- only declare the variable for now

script.Parent.Parent.ClickDetector.MouseClick:Connect(function()
	if not debounce then
		debounce = true
		playing = Sound.SoundId
		newSongNum = songNum + 1
		Sound.SoundId = queue[newSongNum]
		Sound:Play()
		debounce = false
	end
end)

EDIT: I forgot about the button earlier, so I added it now. Hope this helped :)

6 comments

0

didn't work. this also doesn't take into account the button part of the script, which changes the songs when its pressed :/

0

I didn't do the entire script for you. Just the queue generator. Also, what error does the script give?

0

I edited in the button script. How about now bro?

0

still doesnt work :(

0

What kind of error does it give though? I can't fix it if I don't know what's wrong with it, my friend.

0

it doesnt say anything in output :(

Here is the script. Ether put this in server script service as a regular script or in the workspace as a localscript.

--Script By Sabrna563
local SoundFolder = instance.new ("Folder")
SoundFolder.Parent = game.workspace
SoundFolder.Name = Sounds
local Sound1 = instance.new ("Sound")
Sound1.parent = SoundFolder
Sound1.sound id = --Insert first sound link here
local Sound2 = instance.new ("Sound")
Sound2.parent = SoundFolder
Sound2.sound id = -- Insert another song link here
local Sound3 = instance.new ("Sound")
Sound3.parent = SoundFolder
Sound3.sound id = --Insert sound link here
--if you get the pattern, then keep adding more
local sounds = SoundFolder:GetChildren -- Gets sounds
local nsnds = #sounds

Playing = sounds.[math.random(1,nsnds]
Playing.Playing = true

if you want this to loop then add this at the end

SoundFolder:clearallchildren

SoundFolder:destroy --This clears all sounds and folder

and this too

while true do -- This is the loop
	--the script goes here
	wait (0.05) -- Make sure you add a wait or the server will lag!
end -- also when you press enter when you type while true do, the end will automaticly show

3 comments

0

what about the button to change the songs in my script

0

i feel like that doesnt account for the table in my original script among other things

0

I will fix that later

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