Problem with BoomBox and Nametag settings

Hello!

I’ve made a Settings GUI, which also has frames for BoomBox and Nametag.

For BoomBox, it either mutes or unmutes all other boomboxes, but makes the localplayer’s boombox stay unmuted (client-sided).
Better explanation: If I mute boomboxes, I will still hear mine, but won’t hear other’s. You also cannot force anybody to listen to your boombox, as when they mute them, they just wont hear yours, and you won’t hear theirs even if it plays for them when other are muted.

For Nametags, it either makes all (even the localplayer’s) overhead nametags visible or invisible.

Now the problem with nametags is as following: It works perfectly after the button is pressed, but when a new player joins, while their nametag should be invisible (disabled), it stays visible (enabled).

The problem with boomboxes is pretty much the same as nametags, its just that when a new player joins their boombox can still be heared even if it should be muted. But unlike nametags, the boombox itself doesn’t work. I did a little change to the default script of the boombox, and now it won’t even play. The GUI and ID inserting works, however it doesn’t play anything when the ID is entered and when it should play.

Main problem is that I do not get any errors, which is weird.

BoomBox Server script:

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Remote = Tool:WaitForChild("Remote")
local Sound = Handle:WaitForChild("Sound")

function onUnequip()
	Sound:Stop()
end

function onActivate()
	Remote:FireClient(getPlayer(), "ChooseSong")
end

function getPlayer()
	return game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
end

function playSong(id)
	id = id or ""

	local Sound = Handle:WaitForChild('Sound')
	Sound.Looped = true
	Sound.SoundId = "rbxassetid://"..id
	Sound:Play()
end

function onRemote(player, func, ...)
	if player ~= getPlayer() then return end
	
	if func == "Activate" then
		onActivate(...)
	end
	
	if func == "PlaySong" then
		playSong(...)
	end
end

Remote.OnServerEvent:connect(onRemote)
Tool.Unequipped:connect(onUnequip)

Boombox Client Script:

local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local songgui

local CAS = game:GetService("ContextActionService")
local ActionName = "PenguinAttack"

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

function onAction()
	Remote:FireServer("Activate", Mouse.Hit)
end

function onEquipped(mouse)
	--ensure unequip
	onUnequipped()
	
	--bind
	mouse.Button1Down:connect(onAction)
end

function onUnequipped()
	--unbind
	CAS:UnbindAction(ActionName)
	
	if songgui then
		songgui:Destroy()
	end
end

function playAnimation(name, ...)
	local anim = Tool:FindFirstChild(name)
	if anim then
		local human = Tool.Parent:FindFirstChild("Humanoid")
		if human then
			local track = human:LoadAnimation(anim)
			track:Play(...)
		end
	end
end

function chooseSong()
	if Player.PlayerGui:FindFirstChild("ChooseSongGui") then return end
	
	local sg = Instance.new("ScreenGui")
	sg.Name = "ChooseSongGui"
	
	local frame = Instance.new("Frame")
	frame.Style = "RobloxRound"
	frame.Size = UDim2.new(0.25, 0, 0.25, 0)
	frame.Position = UDim2.new((1-frame.Size.X.Scale)/2, 0, (1-frame.Size.Y.Scale)/2, 0)
	frame.Parent = sg
	frame.Draggable = true
	
	local text = Instance.new("TextLabel")
	text.BackgroundTransparency = 1
	text.TextStrokeTransparency = 0
	text.TextColor3 = Color3.new(1, 1, 1)
	text.Size = UDim2.new(1, 0, 0.6, 0)
	text.TextScaled = true
	text.Text = "Lay down the beat!\nPut in the ID number for a song you love that's been uploaded to ROBLOX.\nLeave it blank to stop playing music."
	text.Parent = frame
	
	local input = Instance.new("TextBox")
	input.BackgroundColor3 = Color3.new(0, 0, 0)
	input.BackgroundTransparency = 0.5
	input.BorderColor3 = Color3.new(1, 1, 1)
	input.TextColor3 = Color3.new(1, 1, 1)
	input.TextStrokeTransparency = 1
	input.TextScaled = true
	input.Text = "142376088"
	input.Size = UDim2.new(1, 0, 0.2, 0)
	input.Position = UDim2.new(0, 0, 0.6, 0)
	input.Parent = frame
	
	local button = Instance.new("TextButton")
	button.Style = "RobloxButton"
	button.Size = UDim2.new(0.75, 0, 0.2, 0)
	button.Position = UDim2.new(0.125, 0, 0.8, 0)
	button.TextColor3 = Color3.new(1, 1, 1)
	button.TextStrokeTransparency = 0
	button.Text = "Play!"
	button.TextScaled = true
	button.Parent = frame
	button.MouseButton1Click:connect(function()
		Remote:FireServer("PlaySong", tonumber(input.Text))
		sg:Destroy()
	end)
	
	sg.Parent = Player.PlayerGui
	
	songgui = sg
end

function onRemote(func, ...)
	if func == "PlayAnimation" then
		playAnimation(...)
	end
	
	if func == "ChooseSong" then
		chooseSong()
	end
end

--connect
Tool.Equipped:connect(onEquipped)
Tool.Unequipped:connect(onUnequipped)
Remote.OnClientEvent:connect(onRemote)

BoomBox settings:

local Player = game.Players.LocalPlayer
local Volume = .4
local Connections = {}
local MuteButton = script.Parent.Mute
local UnmuteButton = script.Parent.Unmute


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
		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

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.Status.Text = "Muted"
end)

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

Nametag settings:

local Connections = {}
local value

local function ToggleNametags()
	for i,v in ipairs(game.Players:GetPlayers()) do
		 local Nametag = v.Character:WaitForChild("Head"):FindFirstChild("Overhead")
		 if Nametag then
			Nametag.Enabled = value
		end
	end
end

local function CreateConnection(plr)
		local Nametag = plr.Character:WaitForChild("Head"):FindFirstChild("Overhead")
		if not Connections[plr.Name] then
			Connections[plr.Name] = {}
			table.insert(Connections[plr.Name],
				plr.Character:WaitForChild("Head").ChildAdded:Connect(function(object)
					if object.Name == "Overhead" then
						object.Enabled = value
					end
				end)
		)
		if Nametag then
			table.insert(Connections[plr.Name],
				value.Changed:Connect(function()
					Nametag.Enabled = value
				end)
			)
		end
	end
end

script.Parent.Off.MouseButton1Click:Connect(function()
	script.Parent.Status.OnOff.Text = "Off"
	value = false
	ToggleNametags()
end)
script.Parent.On.MouseButton1Click:Connect(function()
	script.Parent.Status.OnOff.Text = "On"
	value = true
	ToggleNametags()
end)

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

	local Nametag = plr.Character:WaitForChild("Head"):FindFirstChild("Overhead")
	if Nametag then
		Nametag.Enabled = value
		print(value)
	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

If you want to prevent audios from playing for a local player, you can utilize the Changed event. Here’s an example of a LocalScript in game.StarterPlayer.StarterPlayerScripts:

local Audio = --[[Put the audio file reference here, i.e. game.Workspace.Sound]]
Audio.Playing.Changed:Connect(function() -- When the audio starts playing, run this code
Audio.Playing = false -- Stops the audio
end)

For the nametag system, you can utilize the CharacterAdded and PlayerAdded events to always hide the player’s nametags, this should also be in game.StarterPlayer.StarterPlayerScripts of course:

--Firstly, we need to disable all nametags for the first time this script is ran:
local PlayerList = game.Players:GetChildren() -- Assigns all players to a table
for i,v in pairs(PlayerList) do
if game.Workspace:FindFirstChild(v.Name) then -- Makes sure the player is in Workspace
game.Workspace[v.Name].Humanoid.DisplayName = " " -- Hides the player's nametag
end
end
--[[Now that we have hidden all current player's names, we need to account for players
that just died or joined the game as well:]]
local Players = game:GetService("Players") -- Gets the player service
Players.PlayerAdded:Connect(function(player) -- Detects the player and assigns name to "player" variable
	player.CharacterAdded:Connect(function(character) -- Detects the character spawning into the world
		game.Workspace[character.Name].Humanoid.DisplayName = " " -- Hides the name tag
	end)
end)

Hope this helps!

1 Like