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

Why does my music script keep repeating the same song?

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

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

4 comments

0

Thanks, is there anyway to stop it playing the same song twice?

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?

0

You can parent the songs to the player's PlayerGui to make it play locally.

0

Also, I've made it so that it won't play the same song 2 times in a row.

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!

1 comment

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?

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

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

2 comments

0

Either of anyone's scripts will work.

0

What is 'Yada Yada'?

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