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

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

Asked by
Tnipo 10
4 years ago
Edited 4 years ago

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)
0
Can you please put it into the code block to make it easier to reat PoppyandNeivaarecute 134 — 4y
0
Thats better, let me check it out! PoppyandNeivaarecute 134 — 4y
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. Tnipo 10 — 4y
0
Sorry if you misunderstood my explanation before i was trying to say it just wouldnt play. Tnipo 10 — 4y
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. turtle2004 167 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

0
I think script.Music:GetChildren is better Block_manvn 395 — 4y
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 turtle2004 167 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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 :)

Answer this question