Boombox mute not working

Hello! I recently made a GUI, with two buttons. One for muting and second one for unmuting boomboxes.

The system is meant to mute everyone else’s (all other) boomboxes, but leave the local player’s boombox unmuted. Unmute then unmutes all those muted boomboxes. (Players cannot force others to listen to their boomboxes, as when they unmute their boomboxes, it won’t unmute for others, which have boomboxes muted.)

It’s meant to constantly check for all players that are not the local player and mute their boombox, if the Mute button is in effect. (A status text equals to “Muted”)

However, it doesn’t work. Nothing happens, text doesn’t change, boomboxes don’t get muted. The worst thing is that I don’t get any errors about this.

Mute script:

local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	for i, iPlayer in pairs(game.Players:GetPlayers()) do
		if iPlayer.Character:FindFirstChild("BoomBox") then
			while wait() do
				if script.Parent.Parent.Status.OnOff.Text == "Muted" and iPlayer.Name ~= Player.Name then
					iPlayer.Character:WaitForChild("BoomBox").Handle.Sound.Volume = 0
				end
			end
		elseif iPlayer.Backpack:FindFirstChild("BoomBox") then
			while wait() do
				if script.Parent.Parent.Status.OnOff.Text == "Muted" and iPlayer.Name ~= Player.Name then
					iPlayer.Backpack:FindFirstChild("BoomBox").Handle.Sound.Volume = 0
				end
			end
		end
	end
	script.Parent.Parent.Status.OnOff.Text = "Muted"
end)

Unmute script:

script.Parent.MouseButton1Click:Connect(function()
	for i, iPlayer in pairs(game.Players:GetPlayers()) do
		if iPlayer.Character:WaitForChild("BoomBox") then
			iPlayer.Character:FindFirstChild("BoomBox").Handle.Sound.Volume = 0.4
		elseif iPlayer.Backpack:FindFirstChild("BoomBox") then
			iPlayer.Backpack:FindFirstChild("BoomBox").Handle.Sound.Volume = 0.4
		end
	end
	script.Parent.Parent.Status.OnOff.Text = "Unmuted"
end)

GUI:
obrazek

1 Like

I changed the while loop this might be work

This won’t constatly check it though, or will it? Because if a new player would join after the mute button was pressed, their boombox wouldn’t be muted, which I don’t want to happen.

You might want the while loop around the entire function, but even that raises a lot of issues. The problem is you have a for loop for every player, and a infinite loop inside of that. The first player it finds will start a infinite loop and cannot move onto the next player since it is busy repeatedly setting the volume to zero.

I think you should set a while loop outside of the click connection, and toggle it on/off like so

local muted = false

unmuteButton.Activated:Connect(function()
    muted = false
end)

muteButton.Activated:Connect(function()
    muted = true
end)

while task.wait(0.5) do
    for i, player in game.Players:GetPlayers() do
        local boombox = player.Character:FindFirstChild("BoomBox") or player.Backpack:FindFirstChild("BoomBox")
        boombox.Handle.Sound.Volume = if muted then 0 else 10
    end
end
2 Likes

I would recommend doing this instead of using a while loop:

local Player = game.Players.LocalPlayer
local Volume = .4


local function MuteBoomboxes()
	for i,v in ipairs(game.Players:GetPlayers()) do
		if v ~= Player then
			local BoomBox = v.Character:findFirstChild('BoomBox') or v.Backpack:findFirstChild('BoomBox')
			if BoomBox then
				BoomBox.Handle.Sound.Volume = Volume
			end
		end
	end
end


game.Players.PlayerAdded:Connect(function(plr)
	if not plr.Character then
		repeat task.wait() until plr.Character
	end
	
	local BoomBox = plr.Character:findFirstChild('BoomBox') or plr.Backpack:findFirstChild('BoomBox')
	if BoomBox then
		BoomBox.Handle.Sound.Volume = Volume
	end
end)

MuteButton.MouseButton1Click:Connect(function()
	Volume = 0
	MuteBoomboxes()
	script.Parent.Parent.Status.OnOff.Text = "Muted"
end)

UnmuteButton.MouseButton1Click:Connect(function()
	Volume = .4
	MuteBoomboxes()
	script.Parent.Parent.Status.OnOff.Text = "Unmuted"
end)
4 Likes

Thanks! It generally works, however, when a player puts the boombox back to their inventory and takes it out again, then plays a song, I hear it again, even though that Muted is still in effect. (The text is “Muted”). Do you know how that could be fixed?

Yeah, if you did something like this, it should fix that issue:

local Player = game.Players.LocalPlayer
local Volume = .4
local Connections = {}


local function MuteBoomboxes()
	for i,v in ipairs(game.Players:GetPlayers()) do
		if v ~= Player then
			local BoomBox = v.Character:findFirstChild('BoomBox') or v.Backpack:findFirstChild('BoomBox')
			if BoomBox then
				BoomBox.Handle.Sound.Volume = Volume
			end
		end
	end
end


local function CreateConnection(plr)
	if plr ~= Player then
		if not Connections[plr.Name] then
			Connections[plr.Name] = {
				plr.Character.ChildAdded:Connect(function(object)
					if object.Name == 'BoomBox' then
						object.Handle.Sound.Volume = Volume
					end
				end)
			}
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	if not plr.Character then
		repeat task.wait() until plr.Character
	end

	local BoomBox = plr.Character:findFirstChild('BoomBox') or plr.Backpack:findFirstChild('BoomBox')
	if BoomBox then
		BoomBox.Handle.Sound.Volume = Volume
	end
	CreateConnection(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if Connections[plr.Name] then
		for i,v in ipairs(Connections[plr.Name]) do
			v:Disconnect()
		end
		Connections[plr.Name] = nil
	end
end)

for i,v in ipairs(game.Players:GetPlayers()) do
	CreateConnection(v)
end

MuteButton.MouseButton1Click:Connect(function()
	Volume = 0
	MuteBoomboxes()
	script.Parent.Parent.Status.OnOff.Text = "Muted"
end)

UnmuteButton.MouseButton1Click:Connect(function()
	Volume = .4
	MuteBoomboxes()
	script.Parent.Parent.Status.OnOff.Text = "Unmuted"
end)
1 Like

Thanks for your effort, but I believe it still doesn’t work, after I tried it out. I’m not really sure what’s the issue, especially since I don’t get any errors.

:thinking: You’ve tried it with 2 players? It’s set to not mute your boombox at all. Is there something else that’s setting the volume perhaps?

Yes, I tried it with another person. I also looked at the boombox’s script, and it looks like there isn’t anything that affects its volume. Just the volume value in the default sound instance. (Which I don’t think affects anything in this script)

Have you tried pressing Ctrl+Shift+F and typing Volume to see for certain if there’s any other scripts that may be affecting it?

Yeah I did, there shouldn’t be anything connected to the BoomBox.

Okay, well, you could update the CreateConnection function to this

local function CreateConnection(plr)
	if plr ~= Player then
		local BoomBox = plr.Character:findFirstChild('BoomBox') or plr.Backpack:findFirstChild('BoomBox')
		if not Connections[plr.Name] then
			Connections[plr.Name] = {}
			table.insert(Connections[plr.Name],
				plr.Character.ChildAdded:Connect(function(object)
					if object.Name == 'BoomBox' then
						print(Volume)
						object.Handle.Sound.Volume = Volume
					end
				end)
			)
			if BoomBox then
				table.insert(Connections[plr.Name],
					BoomBox.Handle.Sound:GetPropertyChangedSignal('Volume'):Connect(function()
						BoomBox.Handle.Sound.Volume = Volume
					end)
				)
			end
		end
	end
end

and that should fix your issue then – it listens for if the Volume Value changes and sets it to what you’ve set it to whether muted or unmuted. :slight_smile:

I really don’t know what I’m doing wrong… Even the F9 prints are showing the value of “0”, therefore the music shouldn’t be playing. Though it still does.

Is it working at all for you? When is it not?

Is BoomBox named “BoomBox” or something else?

1 Like

It’s still the same as before, it works perfectly first, but when a player puts the BoomBox back to their inventory, then pulls it out and plays a song again, I hear it even on Muted.

And yes, it’s called “BoomBox”.

Do you use Roblox’s default backpack system or do you have your own where you clone the BoomBox? I tested in studio and it worked perfectly as intended. :thinking:

Try making only one GUI, which checks a boolean value for whether the player has the boombox muted or unmuted. This system may work.

What? That doesn’t make a lot of sense?

I use the default Roblox backpack system.