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

local songs = {160442087,705243700}

function playmusic()
    local sound = Instance.new("Sound", game:GetService("Workspace"))
    if sound then
    sound.SoundId = "rbxassetid://"..songs[math.random(1,#songs)]
    sound:Play()
    end
    end

playmusic()

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:

while true do
    -- ...
end

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:

local songs = {
    160442087,
    705243700,
}
local sound
-- Create a new Random.
local random = Random.new()

while true do
    -- Pick and play a random song.
    sound = Instance.new("Sound")
    sound.SoundId = "rbxassetid://" .. songs[random:NextInteger(1, #songs)]
    sound.Parent = workspace
    sound:Play()
end

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

local songs = {
    160442087,
    705243700,
}
local sound
-- Create a new Random.
local random = Random.new()

while true do
    -- Pick and play a random song.
    sound = Instance.new("Sound")
    sound.SoundId = "rbxassetid://" .. songs[random:NextInteger(1, #songs)]
    sound.Parent = workspace
    sound:Play()
    -- Yield until the sound ends.
    sound.Ended:Wait()
end
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:

local songs = {160442087,705243700}


    local sound = Instance.new("Sound", game:GetService("Workspace"))
while true do
    if sound then
    sound.SoundId = "rbxassetid://"..songs[math.random(1,#songs)]
    sound:Play()
wait(sound.Timelength)
    end
    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