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

How do I make a randomly picking audio script?

I'm trying to make an audio playlist that picks a random song and plays it and I thought I had it down but it does absolutely nothing, no errors, nothing.

local audioFolder = game.Workspace.Audios:GetChildren()

function selectSong()
	wait(.2)
	local pickedSong = math.random(1,#audioFolder)
	local namePick = tostring(pickedSong)
	namePick:Play()
	wait(320)
	namePick:Pause()
end
while true do
	wait(.2)
	if audioFolder ~= nil then
		selectSong()
		wait(2)
	else
		print("Audio folder does not exist")
	end
end

Please help, thanks!

3 answers

✓ Accepted answer

You forgot to wrap the table "audioFolder" with these little brackets, "[]". Also, adding a tostring is unnecessary since you want the audio itself, so use these lines instead

local pickedSong = audioFolder[math.random(#audioFolder)]
local namePick = pickedSong

Instead of the sound objects themselves, you could do this.

local soundIds = {28727449, 2928484, 19395, 29284, 38272} -- random numbers
-- put the proper id's in the table

local soundId = soundIds[math.random(1, #soundIds)]

local sound = Instance.new("Sound",game.Workspace)

sound.SoundId = "rbxassetid://"..soundId

pickedSong in your script will only be a number not a song. So you need to access the actual song like so:

local song = audioFolder[pickedSong] -- remember picked song is a number
-- this is just an example

then you should be good to go.

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