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

Song script playing multiple songs, help? [UNANSWERED]

Asked by 9 years ago

I'm making a code for my friend. It plays multiple songs though. Could anyone help me with it?

songs = {165065112, 145170576, 177027430, 153219396, 156643822, 180076355, 146682904, 146558717, 165497101, 143649712}
while true do
    local id = math.random(1, #songs)
    script.Parent.SoundId = "rbxassetid://"..songs[id]
    script.Parent:Play()
    wait(10)
end
0
I'll fix it later. It's pretty late. EzraNehemiah_TF2 3552 — 9y
0
Also, the problem is that the songs needed to be stopped. They'll just keep playing and never stop until the audio is over... Which could take 2 minutes. EzraNehemiah_TF2 3552 — 9y

3 answers

Log in to vote
1
Answered by 9 years ago

Unfortunately, there is no actual method to determine if a sound has finished playing or not.

One way to try is to use the isPlayingproperty.

However, an issue is that if you use :Stop() or :Pause() on this before the sound finishes, then it will also make isPlayingequivalent to false.

The following is one way to do the sound iteration:

songs = {165065112, 145170576, 177027430, 153219396, 156643822, 180076355, 146682904, 146558717, 165497101, 143649712}
script.Parent.Looped = false
while true do
    local id = songs[math.random(1, #songs)]
    script.Parent.SoundId = "rbxassetid://"..tostring(id)
    script.Parent:Play()
    repeat wait() until script.Parent.isPlaying == false
    script.Parent:Stop()
    wait(10)
end

0
The audio isn't supposed to be 10 seconds, the wait(10) is the intermission for playing songs. Grenaderade 525 — 9y
0
Oh. So you want a song to play, and once its done, wait 10 seconds then start a new song? DigitalVeer 1473 — 9y
0
Yes. Grenaderade 525 — 9y
0
I tried to make a solution. Edited. DigitalVeer 1473 — 9y
0
It didn't work, once is plays one song, it doesn't play another. Grenaderade 525 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Apparently, you can now use the TimeLength and TimePosition properties of sounds.

So, if this property now works, then you can say:

songs = {165065112, 145170576, 177027430, 153219396, 156643822, 180076355, 146682904, 146558717, 165497101, 143649712}
while true do
    local id = math.random(1, #songs)
    script.Parent.SoundId = "rbxassetid://"..songs[id]
    script.Parent:Play()
    if script.Parent.TimePosition >= script.Parent.TimeLength then
        script.Parent:Stop()
    end
    wait(10)
end

Hopefully this works...

Log in to vote
-4
Answered by 9 years ago
  1. The id's are strings. Not numbers.
  2. Id becomes a number. Not an ID because you are just choosing a random number 1-the number of ids you have:

To make strings you either need to do this:

songs = {"572647"} --For every single id

or a shorter way

local id = tostring(songs[math.random(1, #songs)])

Now we need to do a bit of tweaking to the script

songs = {165065112, 145170576, 177027430, 153219396, 156643822, 180076355, 146682904, 146558717, 165497101, 143649712}
while true do
    local id = tostring(songs[math.random(1, #songs)])
    script.Parent.SoundId = "rbxassetid://"..songs[id]
    script.Parent:Play()
    wait(10)
end


Hope it helps!

0
Didn't work, the songs are still playing at the same time. Grenaderade 525 — 9y
1
tostring is not necessary: Numbers automatically become strings when concatenated to another number or string. aquathorn321 858 — 9y
0
Wow, I said I was going to fix it later, even if it's not necessary, let's say there is another roblox update and we upgrade to Lua 5.3.2, we would have to use tostring. EzraNehemiah_TF2 3552 — 9y
0
No. In Lua 5.3.2, you can still concatenate integers. DigitalVeer 1473 — 9y

Answer this question