Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I prevent RemoteEvent:FireAllClients() from firing more than once?

I have a script that adds to a counter that all players have by 1 for every round. However, this counter adds more than once sometimes, varying from 1 to however many players there are in the game.

How do I prevent from FireAllClients from firing more than once?

Server-side (the for loop that awards wins)

for _, player in pairs(activePlayers) do
		if player then
			if gameresult == "RunnerWin" and player == runner then
				awardWins(player)
				awardPoints(player, 15)
				print(gameresult)
			elseif gameresult == "PlayersWin" and player ~= runner then
				awardWins(player)
				awardPoints(player, 10)
				print(gameresult)
			else
				print("no one wins")
			end
			event:FireAllClients("RoundEnd", 1)
		end	
	end

Local-side

local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

event.OnClientEvent:connect(function( ... )
	local args = { ... }
	if args[1] == "RoundEnd" then
		local trails = player:FindFirstChild("Trails")
		if trails then
			local pCounter = trails:FindFirstChild("Counters")
			
			if pCounter then
				local counters = pCounter:GetChildren()
				for _, c in pairs(counters) do
					if c:IsA("IntValue") then
						c.Value = c.Value + args[2]
						print("CAPS LOCK ON! I FIRED ONCE")
					end
				end
			end
		end
	end
end)

1 answer

✓ Accepted answer

varying from 1 to however many players there are in the game.

This should be your clue to the error.

In your first block of code, you call FireAllClients() for every Player, rather than just all at once. To fix this, just move line 14 outside of the for loop, or use the singular FireClient call:

for _, player in pairs(activePlayers) do
        if player then
            if gameresult == "RunnerWin" and player == runner then
                awardWins(player)
                awardPoints(player, 15)
                print(gameresult)
            elseif gameresult == "PlayersWin" and player ~= runner then
                awardWins(player)
                awardPoints(player, 10)
                print(gameresult)
            else
                print("no one wins")
            end
		event:FireClient(player, "RoundEnd", 1) --EITHER THIS
        end 
    end

event:FireAllClients("RoundEnd", 1) --OR THIS

This discussion is preserved as it was. New answers, comments, and votes are closed.