Playing Sounds To Specific Players

When anyone presses a button, I want a sound to be played to ONLY the people who are on a specific team. I am currently very confused on how to do this. Should I send my current code?

Don’t put the code in a server script. Create the sound locally per client based on the team.

1 Like

You should send your current script so we can help you with what you already have.

1 Like

You have to fire clients using a remote event, and to determine which clients you have to check their teams, most likely by getting players using the game.Players:GetPlayers(), and after that iterating over the players using a for loop and comparing player.Team to whatever team you need. Pass the sound you want to play in a remote event, and on the client just retrieve the sound and play it.

1 Like

Local Script:

PanicButton.MouseButton1Click:Connect(function()
   RadioEvent2:FireServer("**PANIC BUTTON PRESSED IN " .. string.upper(CurrentChannel) .. "**",CurrentChannel,"panic")
end)

Server Script:

RadioEvent2.OnServerEvent:Connect(function(Player,Message,Channel,Type)
	if Type == "panic" then
		local sound = Instance.new("Sound", game.Workspace)

		sound.Pitch = 1
		sound.SoundId = "rbxassetid://2831166360"
		sound.Volume = 10
		sound.Looped = false

		sound:Play()
		wait(5)
		sound:Destroy()
	end

	if Type == "radio" then
		local sound = Instance.new("Sound", game.Workspace)

		sound.Pitch = 1
		sound.SoundId = "rbxassetid://2600299714"
		sound.Volume = 10
		sound.Looped = false

		sound:Play()
		wait(5)
		sound:Destroy()
	end
end)

You can do Team:GetPlayers() and just loop over that instead of looping over Players:GetPlayers() and checking if a player is in a team.

2 Likes

You are playing the sound on the server; you need to fire a remote event to the client and play the sound on the client.

1 Like

What if I wanted to play the sound to everyone who has a specific GUI?

Then you should use Players:GetPlayers() and check if they have the GUI.

1 Like

But how would I loop through them players? Sorry, I don’t know much about programming lol.

You would use a for loop to loop over the array of players.

1 Like

Okay. One last thing, how would I use the remote event in this case?

You would do RemoteEvent:FireClient() to tell the player to play the sound.

1 Like

So in this case it’s client to server to client?

Yes, as you are using the GuiButton.MouseButton1Click event.

1 Like

I’m confused on how to actually code this. SO far I have:

for i,Player in pairs(game.Players:GetChildren()) do
	if Player.PlayerGui.Radio then
		
	end
end

Yes, you can do it like that but do not use Players:GetChildren(), use Players:GetPlayers(). You can also use ipairs instead of pairs as you are iterating over an array.

1 Like

How would I go about connecting the client to the server to the client?

Localscript

local PlayLocalSoundRemote = game.ReplicatedStorage:WaitForChild("PlayLocalSoundRemote")
PlayLocalSoundRemote.OnClientEvent:Connect(function(SoundId, Volume)
	if Volume == nil then Volume = 1 end
	local Sound = Instance.new("Sound", workspace.CurrentCamera)
	Sound.SoundId = SoundId
	if not Sound.IsLoaded then
		Sound.Loaded:Wait()
	end
	Sound.Volume = Volume
	Sound:Play()
	game:GetService("Debris"):AddItem(Sound, Sound.TimeLength)
end)

ServerScript

local PlayLocalSoundRemote = Instance.new("RemoteEvent", game.ReplicatedStorage)
PlayLocalSoundRemote.Name = "PlayLocalSoundRemote"


function PlaySoundForPlayersWithGui(GuiName, SoundId)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.PlayerGui:FindFirstChild(GuiName) ~= nil then
			PlayLocalSoundRemote:FireClient(v, SoundId)
		end
	end
end


--PlaySoundForPlayersWithGui("Radio", "rbxassetid://1837324424")
6 Likes

Thanks so much! You’re a life saver!

1 Like