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

Why isn't FireAllClients replicating to all clients?

Asked by 3 years ago
Edited 3 years ago

I have code that will send a system message in the chat about who the player is and if they joined or left.

The issue:

I used FireAllClients on the server script and in the chat, it didn't say that I joined the game or anything.

A little help? Thanks!

Server Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("PlayerJoinedorRemoved")

Players.PlayerAdded:Connect(function(playerAdded)
    RemoteEvent:FireAllClients(playerAdded,"Joined") 
end)

Players.PlayerRemoving:Connect(function(playerRemoved)
    RemoteEvent:FireAllClients(playerRemoved,"Left")
end)

Local Script:

local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("PlayerJoinedorRemoved") 

RemoteEvent.OnClientEvent:Connect(function(action) 
    if action == "Joined" then
        game.StarterGui:SetCore("ChatMakeSystemMessage", {  
            Text = (Player.Name.." has joined the server.");
            Color = Color3.fromRGB(121, 255, 188);
            Font = Enum.Font.ArialBold;
        })

    elseif action == "Left" then
        game.StarterGui:SetCore("ChatMakeSystemMessage", {  
            Text = (Player.Name.." has left the server.");
            Color = Color3.fromRGB(255, 51, 71);
            Font = Enum.Font.ArialBold;
        })
    end
end)

1 answer

Log in to vote
1
Answered by 3 years ago

:FireAllClients() does not have ANY required arguments, which means you don't need your playerAdded or playerRemoved. I'm not saying that you can't send them through the remote (you need to in this instance, as I'll show below), but that means that you have to adjust your OnClientEvent's parameters, again, as shown below.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("PlayerJoinedorRemoved")

Players.PlayerAdded:Connect(function(playerAdded)
    RemoteEvent:FireAllClients(playerAdded,"Joined") 
end)

Players.PlayerRemoving:Connect(function(playerRemoved)
    RemoteEvent:FireAllClients(playerRemoved,"Left")
end)

local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("PlayerJoinedorRemoved") 

RemoteEvent.OnClientEvent:Connect(function(playerToDisplay,action) 
    if action == "Joined" then
        game.StarterGui:SetCore("ChatMakeSystemMessage", {  
            Text = (playerToDisplay.Name.." has joined the server.");
            Color = Color3.fromRGB(121, 255, 188);
            Font = Enum.Font.ArialBold;
        })

    elseif action == "Left" then
        game.StarterGui:SetCore("ChatMakeSystemMessage", {  
            Text = (playerToDisplay.Name.." has left the server.");
            Color = Color3.fromRGB(255, 51, 71);
            Font = Enum.Font.ArialBold;
        })
    end
end)

So let's go over what really happens in the local script. When you define LocalPlayer, it is the player that the remote event is sending to. In this case, that is EVERY player, because you are firing all clients. This means that you need to store the player who joined (which we did in the PlayerAdded event). Of course, we do the same for PlayerRemoving. Next, we adjusted the parameters for OnClientEvent.

There are no required arguments for :FireAllClients() so that means anything else you send is being sent as a variant, so it will need to be placed as a parameter for OnClientEvent. Otherwise, it will disturb the order of the parameters. This means that in your original code, when you used action as the parameter for OnClientEvent, it wasn't really Joined or Left. Instead, it was the player that joined or left.

Let me know if you have any further questions!

0
instead of using playertodisplay.Name you can instead just send the player's name as an argument and you can also set that entire table of arguments in the localscript to an argument in the script MLGwarfare04 183 — 3y
Ad

Answer this question