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

How do I make it so when the song ends it restarts?

Asked by
Creacoz 210 Moderation Voter
6 years ago
Edited 6 years ago

So i've done a script so it plays music from a table and I just don't know how to make it choose again from the table when it ends

01local songs = {160442087,705243700}
02 
03function playmusic()
04    local sound = Instance.new("Sound", game:GetService("Workspace"))
05    if sound then
06    sound.SoundId = "rbxassetid://"..songs[math.random(1,#songs)]
07    sound:Play()
08    end
09    end
10 
11playmusic()
0
How many songs do you have? CaptainD_veloper 290 — 6y
0
Try the new script my edited script. CaptainD_veloper 290 — 6y

2 answers

Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
6 years ago
Edited 6 years ago

Firstly, let's use an infinite loop (while true do ... end) to have the music system function indefinitely:

1while true do
2    -- ...
3end

Now, we'll pick and play a random song at the beginning of the statement body. Instead of using math.random, we'll use the recommended Random object; the algorithm provides more randomised numbers. According to some developers, math.random has been updated to use this new algorithm anyway, but I cannot confirm this. We'll also refrain from using the second parent parameter of Instance.new, as it is deprecated:

01local songs = {
02    160442087,
03    705243700,
04}
05local sound
06-- Create a new Random.
07local random = Random.new()
08 
09while true do
10    -- Pick and play a random song.
11    sound = Instance.new("Sound")
12    sound.SoundId = "rbxassetid://" .. songs[random:NextInteger(1, #songs)]
13    sound.Parent = workspace
14    sound:Play()
15end

Lastly, let's use the Sound.Ended event to wait until the song ends. Once it ends, we'll start again:

01local songs = {
02    160442087,
03    705243700,
04}
05local sound
06-- Create a new Random.
07local random = Random.new()
08 
09while true do
10    -- Pick and play a random song.
11    sound = Instance.new("Sound")
12    sound.SoundId = "rbxassetid://" .. songs[random:NextInteger(1, #songs)]
13    sound.Parent = workspace
14    sound:Play()
15    -- Yield until the sound ends.
16    sound.Ended:Wait()
17end
0
Wish I could upvote this! Thank you! Creacoz 210 — 6y
0
No worries! :) You can accept the answer if you think it answered your question to help me out instead BenSBk 781 — 6y
0
this is messy, no offence Ziffixture 6913 — 6y
0
Which part of it is messy? BenSBk 781 — 6y
View all comments (3 more)
0
How do I accept it? Creacoz 210 — 6y
0
Click "Accept Answer" BenSBk 781 — 6y
0
Haha couldnt see it , had to go on edge idk why Creacoz 210 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I have a script but this changes your table. Try using this script:

01local songs = {160442087,705243700}
02 
03 
04    local sound = Instance.new("Sound", game:GetService("Workspace"))
05while true do
06    if sound then
07    sound.SoundId = "rbxassetid://"..songs[math.random(1,#songs)]
08    sound:Play()
09wait(sound.Timelength)
10    end
11    end
0
Sound:Play does not yield BenSBk 781 — 6y
0
This also does not relate to his request BenSBk 781 — 6y

Answer this question