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

Music still playing after scripted to stop?

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.

1 comment

0

You can use :Stop() function to stop music

1 answer

✓ Accepted answer

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)

6 comments

0

Sorry, but I don't really understand why you're using the joined event because that would mess the actual script up. Anyways, even when I did that apart from the joined event (instead I made a variable for the player) it said " FireClient: player argument must be a Player object" could you help?

0

Also I tried to use a character variable instead but it said "attempt to index nil with Character"

0

Is the server script listening for the Joined RemoveEvent? I may have incorrectly assumed that everything in the server script was nested in the listener.

0

I'm not exactly sure why you'd play a song locally and then switch the song on the server. Couldn't you swap out the songs locally together? Perhaps after the wait(3) you could fire an event that stops Lobby and plays Game1 on the client?

0

But what my problem is, is that its not even going to that part because of the errors I said previously, i'm kind of getting confused. Sorry, i'm not that familiar with some parts of scripting

0

Oh wait, I figured it out! What I did was, I made it fire a remote event when you pressed the play button, and on the server i made it play the music when it was fired. Thank you for helping me figure it out!

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