Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by
OniiCh_n 410 Moderation Voter
9 years ago

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

Log in to vote
3
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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
Ad

Answer this question