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.
You should send your current script so we can help you with what you already have.
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.
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.
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.
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.
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.
Okay. One last thing, how would I use the remote event in this case?
So in this case it’s client to server to client?
Yes, as you are using the GuiButton.MouseButton1Click
event.
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.
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")
Thanks so much! You’re a life saver!