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
01 | local Players = game:GetService( "Players" ) |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 | local Remote = Instance.new( "RemoteEvent" ) |
04 | Remote.Name = "ChatHandler" |
05 | Remote.Parent = ReplicatedStorage |
07 | Players.PlayerAdded:Connect( function (player) |
08 | Remote:FireClient(player, "Added" ) |
11 | Players.PlayerRemoving:Connect( function (player) |
12 | Remote:FireClient(player, "Removed" ) |
Local Script
01 | local StarterGui = game:GetService( "StarterGui" ) |
02 | local Players = game:GetService( "Players" ) |
03 | local Player = Players.LocalPlayer |
04 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
05 | local Remote = ReplicatedStorage:WaitForChild( "ChatHandler" ) |
07 | Remote.OnClientEvent:Connect( function (action) |
08 | if action = = "Added" then |
09 | game.StarterGui:SetCore( "ChatMakeSystemMessage" , { |
10 | Text = (Player.Name.. " has joined the server." ); |
11 | Color = Color 3. fromRGB( 97 , 131 , 255 ); |
12 | Font = Enum.Font.SciFi |
14 | elseif action = = "Removed" then |
15 | game.StarterGui:SetCore( "ChatMakeSystemMessage" , { |
16 | Text = (Player.Name.. " has left the server." ); |
17 | Color = Color 3. fromRGB( 97 , 131 , 255 ); |
18 | Font = Enum.Font.SciFi |
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!