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

Why does my music script keep playing music when more people join?

When other people join the game, the script plays music again and it overlaps the sounds, making it a pain to listen to.

while wait() do
local song=math.random(1,5);
        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

I have also tried this:

function startSong()
    local isStarted1 = false
    local isStarted2 = false
    while true do
        if not isStarted1 then
            game.Workspace.song1name:Play()
            isStarted1 = true
            wait(song1length)
            game.Workspace.song1name:Stop()
            isStarted1 = false
        elseif not isStarted 2 then
            game.Workspace.song1name:play()
            isStarted2 = true
            wait(song2length)
            game.Workspace.song2name:Stop()
            isStarted2 = false
        end
        wait(1) -- Wait 1 second before relooping again, or it will crash for StackOverflow (infinite loop)
    end
end
startSong()

6 comments

0

Is this inside the StarterGui? If not, I don't see why it's keeps playing music when more people join.

0

Post the entire script.

0

Those are the ENTIRE Scripts.

0

And yes, it is in the StarterGUI.

0

That's the reason why. Try moving it to workspace. Or are you trying to play it just for the player?

0

Its for all players. But I'm trying to make it so that all the players universally here ONE tune together.

2 answers

✓ Accepted answer

Try this, put the Script (not Localscript) in game.ServerScriptService:

local baseurl = "http://www.roblox.com/asset/?id="
local songs = {"151132026","153519026","147255650","142993375","142833359"}
local audio = Instance.new("Sound", workspace)
audio.Looped = false
audio.Volume = 1
function playMusic()
audio.SoundId = baseurl .. songs[math.random(1, #songs)]
audio:Play()
wait(120)
audio:Stop()
playMusic()
end
playMusic()

By the way, where it says local songs = {"151132026","153519026","147255650","142993375","142833359"} , you can add more Ids to that list at anytime, and it would still work.

UPDATE:

I heard you were still having issues. Try this and play solo. It's a simplified version with different sound. If it doesn't work, another script is messing up the sound:

local baseurl = "rbxasset://"
local songs = {"sounds/victory.wav", "sounds/electronicpingshort.wav"}
local audio = Instance.new("Sound", workspace)
audio.Looped = false
audio.Volume = 1
function playMusic()
audio.SoundId = baseurl .. songs[math.random(1, #songs)]
audio:Play()
wait(1)
audio:Stop()
playMusic()
end
playMusic()

The sound is in the Workspace which is actually the cause the problem. If you put the music at the Workspace then it's played in every place where you are. Because of that it is set to play music at the Workspace then the player joins the game then the sound gets mixed together and that makes the problem so instead of putting the sound at the Workspace then place it at the PlayerGui which causes it to only play music at the client without sound is coming from other players.

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