qLostful
(ethan)
November 14, 2020, 9:44pm
#1
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
Weldify
(Weldify)
November 14, 2020, 9:46pm
#2
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
qLostful
(ethan)
November 14, 2020, 9:55pm
#3
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.
Weldify
(Weldify)
November 14, 2020, 9:56pm
#4
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
xZylter
(xZylter)
November 14, 2020, 9:58pm
#6
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
Weldify
(Weldify)
November 14, 2020, 9:58pm
#7
I believe that’s not what he wants to achieve. Plus PlayLocalSound() is really stiff.
1 Like
qLostful
(ethan)
November 14, 2020, 9:59pm
#8
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
Weldify
(Weldify)
November 14, 2020, 10:01pm
#11
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
qLostful
(ethan)
November 14, 2020, 10:03pm
#12
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
qLostful
(ethan)
November 14, 2020, 10:05pm
#15
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
qLostful
(ethan)
November 14, 2020, 10:08pm
#17
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
qLostful
(ethan)
November 14, 2020, 10:11pm
#19
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