Event fires for all players

  1. **What do you want to achieve? When a player presses the C key or a GUI button, I would like a sound to play and UI to warn their team of danger.

  2. **What is the issue? When I run the script on the server-side, it replicates for each player in the server, so if there were three players, it would repeat 3 times for each player instead of only once per player. When I run it on the client-side, it only appears once, but only on the player who fired it.

  3. **What solutions have you tried so far? running print statements and trying to run it on the client with a local script.

Here is the local script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local db = false 
local frame = script.Parent
local danger = frame.Danger
local help = frame.Help
local attack = frame.Attack
local taunt = frame.Taunt
local debris = game:GetService("Debris")
local rs = game:GetService("ReplicatedStorage")

local function danger(inputObject, gameProcessedEvent)
	if gameProcessedEvent then 	
		return 
	end
	if inputObject.KeyCode == Enum.KeyCode.C and db == false then
		game.ReplicatedStorage.RemoteEvents.Danger:FireServer()
		db = true
		wait(5)
		db = false
	end
end

UserInputService.InputBegan:Connect(danger)

danger.MouseButton1Click:Connect(function()
	if db == false then
		game.ReplicatedStorage.RemoteEvents.Danger:FireServer()
		db = true
		wait(5)
		db = false
	end
end)

Here is the server script:

local player = script.Parent.Parent.Parent.Parent
local stats = player:WaitForChild("leaderstats")
local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

game.ReplicatedStorage.RemoteEvents.Danger.OnServerEvent:Connect(function(player)
	player.Character.Head.Danger:Play()
	for i, v in pairs(Players:GetPlayers()) do
		if v.Team.TeamColor == player.Team.TeamColor then
			local comms = rs.UI.Comms:Clone()
			comms.Title.Text = player.Name
			if v.Team.TeamColor == game.Teams.Red.TeamColor then
				comms.Title.TextColor3 = Color3.new(1,0,0)
			elseif v.Team.TeamColor == game.Teams.Blue.TeamColor then
				comms.Title.TextColor3 = Color3.new(0,0,1)
			elseif v.Team.TeamColor == game.Teams.Green.TeamColor then
				comms.Title.TextColor3 = Color3.new(0,1,0)
			end	
			comms.Action.TextColor3 = Color3.new(1,1,0)
			comms.Action.Text = " wants to retreat!"
			comms.Parent = v.PlayerGui.Comms.Comms
			debris:AddItem(comms,3)
		end
	end
end)

For the sound, I think it should be in the local script

I could do that, but the main issue is the UI.
When I press C once, this is what happens:
screenshot

What I suggest you do is utilize remote events. When the player presses c, play the sound locally for the player who fired it (if you want it to seem smooth) and then fire a remote to the server.

Have the server listen for that event and either fireAllClients(if you didn’t play the sound locally for the player who pressed c) or loop through the player list and fire every other player except for the one who triggered it (so it doesn’t play twice).

Note if you wish for certain people to hear the sound than you would have to do what you did in your current script and loop through the player list + check whatever you need to (such as team, etc). But this time instead of playing the sound on the server you just fire each client and do the below:

Lastly, have the client listen to the same remote and play the desired sound.

I was able to switch the scripts around and do an OnClientEvent with FireAllClients which works perfectly.