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

How do I make a randomly picking audio script?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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
Ad
Log in to vote
0
Answered by 6 years ago

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
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

Answer this question