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

Song script playing multiple songs, help? [UNANSWERED]

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

2 comments

0

I'll fix it later. It's pretty late.

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.

3 answers

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

One way to try is to use the isPlaying property.

However, an issue is that if you use :Stop() or :Pause() on this before the sound finishes, then it will also make isPlaying equivalent 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

5 comments

0

The audio isn't supposed to be 10 seconds, the wait(10) is the intermission for playing songs.

0

Oh. So you want a song to play, and once its done, wait 10 seconds then start a new song?

0

Yes.

0

I tried to make a solution. Edited.

0

It didn't work, once is plays one song, it doesn't play another.

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...

  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!

4 comments

0

Didn't work, the songs are still playing at the same time.

1

tostring is not necessary: Numbers automatically become strings when concatenated to another number or string.

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.

0

No. In Lua 5.3.2, you can still concatenate integers.

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