This tutorial was entirely rewritten from scratch from the suggestions I received in the replies below. I hope all of you find it more convienient.
This tutorial assumes you know the basics of scripting.
As many of you know, Roblox only provides a message for when one of your friend joins your experience by default. This resource will explain how you can make a Player Join and Leave message for every single person who joins a server in your experience.
This tutorial will be divided into 2 sections.
Section One will be Join and Leave Messages that will take place in the Chat Channel where everyone chats in.
Section Two will be Join and Leave Messages that will take place in an entirely different Chat Channel for convenience.
I would recommend Part One for Beginning Scripters and Part Two for more experienced scripters.
Section 1 (For Beginners)
Let me give sort of a background first.
Some actions in scripts can only be performed by a server and other actions only by a client. For example, a player (client) may activate a GUI button, upon which the server needs to enact a game-wide change. In these cases we’ll need a RemoteEvent
or RemoteFunction
.
A RemoteEvent is designed to provide a one-way message between the server and clients, allowing Scripts
to call code in LocalScripts
and vice-versa. This message can be directed from one client to the server, from the server to a particular client, or from the server to all clients.
Now that you understand what RemoteEvents are we can start scripting.
First, you’re going to have to put a RemoteEvent inside ReplicatedStorage for this Section of the tutorial.
Insert a RemoteEvent into ReplicatedStorage.
You can name the RemoteEvent whatever you want, but for the convenience of this tutorial, you can name it PlayerEvent
Next, you need to insert a script into ServerScriptService. The name of this script doesn’t matter.
Inside the script you’ll want to define the variables for the services you want to use for the Join and Leave Message.
-- Variables Defined for the Services used in the script
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Since the RemoteEvent we added earlier is in Replicated Storage, we're going to need to use ReplicatedStorage in the script.
local Players = game:GetService("Players") -- This Tutorial is about **Players** Joining and Leaving your game Right?
We’ll also need to make variables for the RemoteEvent we inserted in ReplicatedStorage earlier:
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Since the RemoteEvent we added earlier is in Replicated Storage, we're going to need to use ReplicatedStorage in the script.
local Players = game:GetService("Players") -- This Tutorial is about **Players** Joining and Leaving your game Right?
local PlayerEvent = ReplicatedStorage.PlayerEvent -- Variable defined for the RemoteEvent we added earlier.
Next, we’ll need to activate or “Fire” the RemoteEvent when the player joins and leaves the server.
-- Fire the Event when the Player Joins
Players.PlayerAdded:Connect(function(Player)
PlayerEvent:FireAllClients(Player.Name, "OnPlayerJoined")
end
-- Fire the Event when the Player Leaves
Players.PlayerAdded:Connect(function(Player)
PlayerEvent:FireAllClients(Player.Name, "OnPlayerLeave")
end
-- We fire the event with the arguments Player.Name because we want the player's name to appear in the chat with the Join and Leave Message.
The End Result of the First Script should look like this:
-- Variables Defined for the Services used in the script
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Since the RemoteEvent we added earlier is in Replicated Storage, we're going to need to use ReplicatedStorage in the script.
local Players = game:GetService("Players") -- This Tutorial is about **Players** Joining and Leaving your game Right?
-- Variables for the RemoteEvents added earlier
local PlayerEvent = ReplicatedStorage.PlayerEvent
-- Fire the Event when the Player Joins
Players.PlayerAdded:Connect(function(Player)
PlayerEvent:FireAllClients(Player.Name, "OnPlayerJoined")
end
-- Fire the Event when the Player Leaves
Players.PlayerRemoving:Connect(function(Player)
PlayerEvent:FireAllClients(Player.Name, "OnPlayerLeft")
end
Now we’ll need to make a local script that will make the messages appear when a player joins and leaves in the chat.
Insert a Local Script in StarterPlayerScripts. StarterPlayerScripts is located in the StarterPlayer Folder in Roblox Studio’s Explorer.
Type in the following code into the Local Script you just created:
-- Once again defining variables for all the Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui") -- We're going to use the StarterGui in this script since the Chat Messages are a part of the StarterGui.
local Players = game:GetService("Players")
-- Defining the variable for the RemoteEvent again
local PlayerEvent = ReplicatedStorage.PlayerEvent
Now we will be creating the Join and Leave messages.
The join and leave messages are customizable by Message Color, Font, Font Size, and the Join and Leave Message Text.
PlayerEvent.OnClientEvent:Connect(function(player, state)
if state == "OnPlayerJoined" then
-- The message below will appear in the Chat when a Player Joins. The message is completely customizable by Font, Text Color, and Text Size!
StarterGui:SetCore("ChatMakeSystemMessage", {
Text = "{System}: "..player.." has joined the game.",
Color = Color3.new(1, 1, 1)
})
elseif state == "OnPlayerLeft" then
StarterGui:SetCore("ChatMakeSystemMessage", {
-- The message below will appear in the Chat when a Player Leaves. The message is completely customizable by Font, Text Color, and Text Size!
Text = "{System}: "..player.." has left the game.",
Color = Color3.new(1, 1, 1)
})
end
end
You may have noticed that the other argument when we fired the Remote Event in the first script was “OnPlayerJoined” and “OnPlayerLeft”.
When the RemoteEvent fires for when a Player Joins, the player’s “state” was considered “OnPlayerJoined” thus creating the Join Message.
Its the same concept for when a player leaves.
When the RemoteEvent fires for when a Player Leaves, the player’s “state” was considered “OnPlayerLeft” thus creating the Leave Message.
The final result of the local script should be this:
-- Once again defining variables for all the Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui") -- We're going to use the StarterGui in this script since the Chat Messages are a part of the StarterGui.
local Players = game:GetService("Players")
-- Defining the variable for the RemoteEvent again
local PlayerEvent = ReplicatedStorage.PlayerEvent
PlayerEvent.OnClientEvent:Connect(function(player, state)
if state == "OnPlayerJoined" then
-- The message below will appear in the Chat when a Player Joins. The message is completely customizable by Font, Text Color, and Text Size!
StarterGui:SetCore("ChatMakeSystemMessage", {
Text = "{System}: "..player.." has joined the game.",
Color = Color3.new(1, 1, 1)
})
elseif state == "OnPlayerLeft" then
StarterGui:SetCore("ChatMakeSystemMessage", {
-- The message below will appear in the Chat when a Player Leaves. The message is completely customizable by Font, Text Color, and Text Size!
Text = "{System}: "..player.." has left the game.",
Color = Color3.new(1, 1, 1)
})
end
end
And that's it! Test it out with someone else and it should work!
Section 2 (Intermediate)
This part of the tutorial assumes that you have a basic knowledge of scripting and Remote Events and an intermediate knowledge of Roblox’s Lua Chat System.
This section of the tutorial displays how you can make a more efficient version of Join and Leave Messages.
Some of you might be bothered that the Join and Leave messages are all in one channel, which is the main one, where everyone in the server speaks.
To prevent the main chat from being too cluttered, it would be better if we put the Join and Leaves Messages in a separate Chat channel to make the chat neater.
First we’re going to make it so we can see all the channels in chat so you’ll know what I mean.
Create a Local Script in StarterPlayerScripts. The name of the local script doesn’t matter.
The script below Gets All the modules we need to modify the Chat Settings.
local Chat = game:GetService("Chat")
local ClientChatModules = Chat:WaitForChild("ClientChatModules")
local ChatSettings = require(ClientChatModules:WaitForChild("ChatSettings"))
Now we can modify the default settings for the chat. In this case we’ll want the Channel Bar to show
ChatSettings.ShowChannelsBar = true
The final code in the local script should look like this:
local Chat = game:GetService("Chat")
local ClientChatModules = Chat:WaitForChild("ClientChatModules")
local ChatSettings = require(ClientChatModules:WaitForChild("ChatSettings"))
ChatSettings.ShowChannelsBar = true
After you finish writing the code, you will no longer need the local script open. You can now close it.
Play test in Studio, and when you’re in the game, open the chat and should see Bars that look like this:
These are the default Chat channels. These chat channels were always there, but the bars just weren’t visible.
You can now end the play test.
Its time we make the Join and Leave Messages. They’re going to appear in the System Channel so the chat will be more neater, which is the purpose of this section. To switch to the System Channel, simply click the System Channel Button on the Channel Bar.
Create a server script and place it in ServerScriptService. Once again, the name of the script doesn’t matter.
The following code defines the Roblox services we’re using for our script. We’ll need ServerScriptService and Players in this case along with the ChatService. We use WaitForChild because ChatServiceRunner only appears in ServerScriptService when we’re in the game.
We’ll also need to get the System Channel so the Join and Leave messages will appear in the System Chat Channel Only.
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ChatServiceRunner = ServerScriptService:WaitForChild("ChatServiceRunner")
local ChatService = require(ChatServiceRunner.ChatService)
local systemChannel = ChatService:GetChannel("System")
Now for the join and leave messages. They’re similar to the Join and Leave Messages from Section One, but this time they’ll appear in the System Channel.
-- When A Player Joins, the Join Message Appears in the System Channel
local function playerAdded(player)
systemChannel:SendSystemMessage("{System}: " .. player.Name .. " has joined the game.")
end
-- When A Player Leaves, the Leaves Message Appears in the System Channel as well
local function playerRemoved(player)
systemChannel:SendSystemMessage("{System}: " .. player.Name .. " has left the game.")
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoved)
The final code in the script should be this:
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ChatServiceRunner = ServerScriptService:WaitForChild("ChatServiceRunner")
local ChatService = require(ChatServiceRunner.ChatService)
local systemChannel = ChatService:GetChannel("System")
local function playerAdded(player)
systemChannel:SendSystemMessage("{System}: " .. player.Name .. " has joined the game.")
end
local function playerRemoved(player)
systemChannel:SendSystemMessage("{System}: " .. player.Name .. " has left the game.")
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoved)
And That’s it!
You can now play test in Studio, and when you do, the join and leave messages should appear in the System Channel. There will also be a little red icon that appears on the Channel Bar that lets you know someone joined and left.
I hoped this tutorial helped you. If you have any other questions, concerns, or if you ran into any issues, please let me know.
Credits for the rewrite: