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

How would i take a math.random that was selected and play the audio selected?

I'm making this song system for my game and i made a gui that randomly selects one of the songs in the folders in the gui, so how would i play the selected song?

-- The songs are in the folder which the script in the gui selects.

-- Script in the gui

local Player	= game.Players.LocalPlayer

Songs			= script["Music"]:GetChildren()
Selected		= nil

MTL				= .25

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function Song()
Random = Songs[math.random(1,#Songs)]
Selected = Random
script.Parent.Playing.Playing.Text = Selected.Name
wait()
for i = 1,#Selected:GetChildren(),1 do
Selected:GetChildren()[i]:Play()
wait(Selected:GetChildren()[i].Duration.Value-MTL)
end
end

while true do Song() end

Player.Character.Humanoid.Died:connect(function()
for Index,A in pairs(Selected:GetChildren()) do
if A:IsA("Sound") then
A:stop()
end
end
end)

5 comments

0

Can you please put it into the code block to make it easier to reat

0

Thats better, let me check it out!

0

Oh by the way it does say the selected one will play but it doesnt no errors pop up and it doesnt play in game.

0

Sorry if you misunderstood my explanation before i was trying to say it just wouldnt play.

0

please address your presentation, if you want to progress to a comprehensive level or wish to work with teams in the future, you won't get far with presentation like this. I appreciate that it can be hard to learn by your own means, but it will benefit you and others in the future.

2 answers

✓ Accepted answer

Here is what I would do:

while true do 
     local songs = {script.Music.MarshmallowAlone,It'sRainingTacos} -- put your song names in there
     local chosen = songs[math.random(1,#songs)]

    chosen:Play()
    repeat
end

Be aware I am new to scripting so this may not work.

If this helped please accept this answer.

2 comments

0

I think script.Music:GetChildren is better

0

May I point out that you have no need to be declaring the 'songs; variable within the loop. Furthermore, 'repeat' is another type of loop in itself, and has no place within the code you have provided

In line 17, instead using normal wait() function (which is use to wait for a number of time), we can do like this: Selected:GetChildren()[i].Ended:Wait() This will cause the script wait until the song end and continue.

*This is the first time I answer someone is question. If there is any issue, don't harm me :)

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