Playing Sounds To Specific Players

I have this code in a server script to play a sound to everyone:

local sound = Instance.new("Sound", game.Workspace)
	
sound.Pitch = 1
sound.SoundId = "rbxassetid://2831166360"
sound.Volume = 10
sound.Looped = false
	
sound:Play()
wait(10)
sound:Destroy()

However, I only want it to play for people on a specific team. How would I do this?

4 Likes

You can use a remote event for that. Pass the sound as an argument and on the client play the sound that was passed.

2 Likes

I am already using a remote event to play it to everyone, but I want to know how to ONLY play it to people on a specific team.

Wrap everything in this

if game.Players.LocalPlayer.Team == someteam then 
end

Replace someteam with the team that you want to hear the sound.

2 Likes

You are doing :FireAllClients()

For individual players you must use Player:FireClient()

2 Likes

SoundService has a built in function just for this need. The Developer Hub should explain it well enough. Alongside with RemoteEvents you should be able to communicate to clients which should play the sound.

1 Like

I believe that’s not what he wants to achieve. Plus PlayLocalSound() is really stiff.

1 Like

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