Basically, I'm making a menu screen for my PVP game, and whenever a round starts i made it so that the lobby music stops and the game music starts. The problem is, the lobby music never stops the first time, so the game music plays along but the second time it does? Here are the scripts
Local script in startergui:
-- This is for the menu screen playBtn.MouseButton1Click:Connect(function() camera.CameraType = Enum.CameraType.Custom game.ReplicatedStorage.Joined:FireServer() script.Parent.Parent.Afk.Enabled = true script.Parent.Parent.ScreenGui.Enabled = true script.Parent.Parent.SpectateGui.Enabled = true script.Parent.Parent.Bugs.Enabled = true script.Parent.Parent.DonateGui.Enabled = true script.Parent.Parent.Shop.Enabled = true Char.HumanoidRootPart.Anchored = false game.Workspace.Music.Lobby:Play() -- Lobby is the music script.Parent:Destroy() end)
And now, here is the script in serverscriptservice:
local plrs = {} for i, player in pairs(game.Players:GetPlayers()) do if player:FindFirstChild("Playing") and player then table.insert(plrs,player) end end wait(2) game.Workspace.Lobby.Floor2.Transparency = 0.7 game.Workspace.Lobby.Floor2.Material = "Glass" game.Workspace.Lobby.Floor2.BrickColor = BrickColor.new('Medium blue') wait(3) -- Where the lobby music stops game.Workspace.Music.Lobby.Playing = false game.Workspace.Music.Game1:Play()
Help would be greatly appreciated.
You're playing the sound locally and then stopping it on the server. The client and the server are two different spaces, therefore this does not work.
script.Parent.Parent.Shop.Enabled = true Char.HumanoidRootPart.Anchored = false game.Workspace.Music.Lobby:Play() --right here -- Lobby is the music script.Parent:Destroy()
However, there is a fix!
playBtn.MouseButton1Click:Connect(function() camera.CameraType = Enum.CameraType.Custom game.ReplicatedStorage.Joined:FireServer() script.Parent.Parent.Afk.Enabled = true script.Parent.Parent.ScreenGui.Enabled = true script.Parent.Parent.SpectateGui.Enabled = true script.Parent.Parent.Bugs.Enabled = true script.Parent.Parent.DonateGui.Enabled = true script.Parent.Parent.Shop.Enabled = true Char.HumanoidRootPart.Anchored = false game.Workspace.Music.Lobby:Play() -- Lobby is the music -- script.Parent:Destroy() we are going want to comment out this if we want to add further events down the line end)
What we can do is remove the line where the script destroys itself and add an event that will wait for server to tell it to stop playing the song. Let's create a new RemoteEvent and name it stopMusic and add in the listener.
Client:
playBtn.MouseButton1Click:Connect(function() camera.CameraType = Enum.CameraType.Custom game.ReplicatedStorage.Joined:FireServer() script.Parent.Parent.Afk.Enabled = true script.Parent.Parent.ScreenGui.Enabled = true script.Parent.Parent.SpectateGui.Enabled = true script.Parent.Parent.Bugs.Enabled = true script.Parent.Parent.DonateGui.Enabled = true script.Parent.Parent.Shop.Enabled = true Char.HumanoidRootPart.Anchored = false game.Workspace.Music.Lobby:Play() -- Lobby is the music end) game.ReplicatedStorage.stopMusic.OnClientEvent:Connect(function() game.Workspace.Music.Lobby:Stop() -- you can also do game.Workspace.Music.Lobby.Playing=false, it will also stop the song script:Destroy() end)
Server:
game.ReplicatedStorage.Joined.OnServerEvent:Connect(function(player) -- I'm assuming this is the listener for the server portion local plrs = {} for i, player in pairs(game.Players:GetPlayers()) do if player:FindFirstChild("Playing") and player then table.insert(plrs,player) end end wait(2) game.Workspace.Lobby.Floor2.Transparency = 0.7 game.Workspace.Lobby.Floor2.Material = "Glass" game.Workspace.Lobby.Floor2.BrickColor = BrickColor.new('Medium blue') wait(3) -- Where the lobby music stops game.ReplicateStorage.stopMusic:FireClient(player) -- communicate with the client to stop the song game.Workspace.Music.Game1:Play() end)