Check through all players backpack?

Hello, so I have this code that I want to have it check all the players in the server to see if they have the “Boombox” tool in their backpack. If one of them does, and the local player clicks the sound off button, the boombox will mute as well as the lobby music. How can I check if a player has a boombox that way I can mute it?

Local Script

local Players = game:GetService("Players")
local Backpack = Players:WaitForChild("Backpack")

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Image == "rbxassetid://13599977876" then
		workspace.Sounds.Lobby:Play()
		Backpack.Boombox.Handle.Sound:Play()
	end
	
	if script.Parent.Image == "rbxassetid://13599968192" then
		workspace.Sounds.Lobby:Stop()
		Backpack.Boombox.Handle.Sound:Stop()
	end
end)
for i, v in ipairs(game.Players:GetPlayers()) do
    if v.Backpack:FindFirstChild("TOOL NAME") do
        print("stuff")
    end
end
1 Like

Hello, thank you for the reply! However it doesn’t seem to work. It gives no errors

this is trying to get backpack of players not of the local player you should use something like this

local Player = game.Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")

Also if you are trying to mute the lobby sound for everyone in the server then you need to fire remote event and let the server mute or either set an attribute or something the clients see to mute the sound with

I would try this script:

-- [Variables] --
local Players = game:GetService("Players")

local Button = script.Parent
local Image = Button.Image

local Sounds = workspace.Sounds
local LobbySounds = Sounds.Lobby

local STOP_ICON = "rbxassetid://13599968192"
local PLAY_ICON = "rbxassetid://13599977876"

-- [Script] --
local function ToggleAudio()
	LobbySounds.Playing = not LobbySounds.Playing

	for _, Player in ipairs(Players:GetPlayers()) do

		local Character = Player.Character
		local Backpack = Player.Backpack

		if (Backpack:FindFirstChild("Boombox") ~= nil) then
			local Tool = Backpack:FindFirstChild("Boombox") 
			local Handle = Tool:WaitForChild("Handle")
			local Sound = Handle:WaitForChild("Sound")

			Sound.Playing = not Sound.Playing
		end

		if (Character ~= nil) and (Character:FindFirstChild("Boombox") ~= nil) then
			local Tool = Character:FindFirstChild("Boombox") 
			local Handle = Tool:WaitForChild("Handle")
			local Sound = Handle:WaitForChild("Sound")

			Sound.Playing = not Sound.Playing
		end
	end
	
	Button.Image = if (Button.Image == STOP_ICON) then
		PLAY_ICON
		else
		STOP_ICON
end

Button.Image = PLAY_ICON
Button.Activated:Connect(ToggleAudio)

NOTE: I also looked through the character because when a tool is equipped it goes into the players character.

Hope this helps :grinning::grinning::grinning:

If all you need to do is mute sounds you should try SoundGroups

Most likely not the best method but this should do.

for _,__ in pairs(game.Players:GetChildren()) do
	for i,v in pairs(__.Backpack:GetChildren()) do 
		if string.find("BoomBox", v.Name) then
			print("code", __.Name)
		end
	end
end

Careful though, when someone equips a tool, it removes the tool from their backpack and is parented to their character. So you would need to check through a player’s character too.