✓ Accepted answer
To achieve this, you have to use a RemoteEvent to work on both the server and the client. The basic layout will be:
Server waits for players to join with PlayerAdded. Server then fires a remote.
This remote will be received by the client, who will create the system message. So how do we do it? Read below for a quick example:
Server Script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent")
Remote.Name = "ChatHandler"
Remote.Parent = ReplicatedStorage
Players.PlayerAdded:Connect(function(player)
Remote:FireClient(player,"Added")
end)
Players.PlayerRemoving:Connect(function(player)
Remote:FireClient(player,"Removed")
end)
Local Script
local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("ChatHandler")
Remote.OnClientEvent:Connect(function(action)
if action == "Added" then
game.StarterGui:SetCore("ChatMakeSystemMessage", {
Text = (Player.Name.." has joined the server.");
Color = Color3.fromRGB(97, 131, 255);
Font = Enum.Font.SciFi
})
elseif action == "Removed" then
game.StarterGui:SetCore("ChatMakeSystemMessage", {
Text = (Player.Name.." has left the server.");
Color = Color3.fromRGB(97, 131, 255);
Font = Enum.Font.SciFi
})
end
end)
To sum it all up, we have to use a remote here, because we have one situation which needs to be covered on the server-side, and another which needs to be covered locally. Thus, we sent our remote from the server to a specific client (the one joining/leaving). In addition, we sent a specific argument with the remote ("Added" or "Removed"). This eliminates the need for having an entirely separate remote event, just to handle the other end of this code (joining vs. leaving).
If you have any questions, feel free to comment.
Hope this helps!
1 comment
You could rely on remote events