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

Why does my music script keep repeating the same song?

Asked by
Scenal 20
10 years ago

Hi, this is the script, and it keeps on playing the first song over and over again- I've checked if it's looped and it isn't. What's wrong?

function startSong()
    local isStarted1 = false
    local isStarted2 = false
    local isStarted3 = false
    local isStarted4 = false
    local isStarted5 = false

 while true do
        if not isStarted1 then
            game.Workspace.Feeling:Play()
            isStarted1 = true
            wait(120)
            game.Workspace.Feeling:Stop()
            isStarted1 = false
        elseif not isStarted2 then
            game.Workspace.Po:play()
            isStarted2 = true
            wait(120)
            game.Workspace.Po:Stop()
            isStarted2 = false
       elseif not isStarted3 then
            game.Workspace.Demons:play()
            isStarted3 = true
            wait(120)
            game.Workspace.Demons:Stop()
            isStarted3 = false
elseif not isStarted4 then
            game.Workspace.Trumpets:play()
            isStarted4 = true
            wait(45)
            game.Workspace.Trumpets:Stop()
            isStarted4 = false
elseif not isStarted5 then
            game.Workspace.Survival:play()
            isStarted5 = true
            wait(100)
            game.Workspace.Survival:Stop()
            isStarted5 = false
        end
        wait(1) 
    end
end
startSong()

4 answers

Log in to vote
1
Answered by 10 years ago

Here you go, try this:

local lastSong=1
while wait() do
repeat song=math.random(1,5) until song~= lastSong
lastSong=song;
        if song==1 then
            game.Workspace.Feeling:Play()
            wait(120)
            game.Workspace.Feeling:Stop()
        elseif song==2 then
            game.Workspace.Po:play()
            wait(120)
            game.Workspace.Po:Stop()
       elseif song==3 then
            game.Workspace.Demons:play()
            wait(120)
            game.Workspace.Demons:Stop()
elseif song==4 then
            game.Workspace.Trumpets:play()
            wait(45)
            game.Workspace.Trumpets:Stop()
elseif song==5 then
            game.Workspace.Survival:play()
            wait(100)
            game.Workspace.Survival:Stop()
        end
end
0
Thanks, is there anyway to stop it playing the same song twice? Scenal 20 — 10y
0
Also, when someone else joins, it repeats the script and overlaps, which makes lots of songs play at once- How do I fix this? Scenal 20 — 10y
0
You can parent the songs to the player's PlayerGui to make it play locally. TheGuyWithAShortName 673 — 10y
0
Also, I've made it so that it won't play the same song 2 times in a row. TheGuyWithAShortName 673 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

It's because you are setting isStarted1 to false after it plays its 120 seconds worth of audio, which then redirects it to the same conditional path (i.e. if not isStarted1 then...)

If you want it to play one after another, cut out the lines where it sets each isStarted value to false, and paste them at the end of the loop, altogether.

Good luck; hope I could be of help! Please consider leaving an upvote!

0
This also glitches, when another person joins, it plays the song again and makes two songs overlap over eachother, how would I fix this? Scenal 20 — 10y
Log in to vote
0
Answered by
Maxomega3 106
10 years ago

Every time the loop runs, it sees that the first loop is false. The loop while loop will only repeat once it's finished.

Just do:

while true do
    Song1:Play ()
    wait (120)
    Song1:Stop ()
    Song2:Play ()
    wait (120)
    Song2:Stop ()
end
Log in to vote
0
Answered by
Bebee2 195
10 years ago

It's because you set isStarted back to false and the condition goes back to the first song.

I suggested doing it like...

local songnumber = 1
if songnumber == 1
--Yada Yada
songnumber = 2
elseif songnumber == 2 then
-- Yada Yada
songnumber = 1
0
Either of anyone's scripts will work. Bebee2 195 — 10y
0
What is 'Yada Yada'? Scenal 20 — 10y

Answer this question