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

How to make system message server-sided?

Asked by 3 years ago
Edited 3 years ago

I have code that will send a system message in the chat about which player joined or left. It uses a RemoteEvent in ReplicatedStorage to send the message.

The problem is that when I join the game and I go on my alt and join the same server, the message about the player who joined was only on that player's side, or client-sided.

How can I make it server-sided?

Codes:

LocalScript:

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

Remote.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)

Server Script:

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

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

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

1 answer

Log in to vote
0
Answered by 3 years ago

The issue is that you're firing the remote event to that specific player that joined, and not everyone else. You should use :FireAllClients to fire the remote event to all the players.

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)
0
I joined the game and nothing appearing in the chat. Is that normal? Green_Lime2 80 — 3y
Ad

Answer this question