How to script "player has left the game" in game chat

Hey! I’m K0rrupt!

I was just wondering: How do I script a “player has left the game” for my game chat,

I already did a “player has joined the game” but I just cant figure out how to do
“player has left the game” because it always shows both joined and left game when a player joins.

Any tips would help!

Here is my code for the “player has joined the game” since I know both joined and left scripts should be similar:

ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild('JoinedPlayer')

local function onPlayerJoin(player)
	RemoteEvent:FireClient(player, player.Name)
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

StarterPlayer → StarterPlayerScripts:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild('JoinedPlayer')
local textColor = BrickColor.new('Lime green')

local function welcome(playerName)
	game.StarterGui:SetCore('ChatMakeSystemMessage', {
		Text = playerName..' has joined the game';
		Font = Enum.Font.SourceSansBold;
		Color = textColor.Color;
		FontSize = Enum.FontSize.Size24;
	})
end

RemoteEvent.OnClientEvent:Connect(welcome)
1 Like

It’s the same thing, but with the PlayerRemoving event.

1 Like

Just use the PlayerRemoving event.

1 Like

So do I create separate scripts and replace it with PlayerRemoving or do I put the PlayerRemoving script under the JoinedPlayer script?

this is an easy bug to fix brb.

not sure where the problem is but create another remote event called playeremoving!

image

You should put it under the PlayerAdded, but you will also need to create another RemoteEvent.

2 Likes

image
That player added?

I was talking of something different, but now that I see you should use :FireAllClients() so every player can see the chat message.

That would look like this:

– Server Script –

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild('JoinedPlayer')
local RemoteEvent2 = ["Here create another RemoteEvent"]

local function onPlayerJoin(player)
	RemoteEvent:FireAllClients(player.Name)
end

local function onPlayerRemove(player)
	RemoteEvent2:FireAllClients(player.Name)
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerRemove)

– Client Script –

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild('JoinedPlayer')
local RemoteEvent2 = ReplicatedStorage:WaitForChild("RemovedPlayer")
local textColor = BrickColor.new('Lime green')

local function welcome(playerName)
	game.StarterGui:SetCore('ChatMakeSystemMessage', {
		Text = playerName..' has joined the game';
		Font = Enum.Font.SourceSansBold;
		Color = textColor.Color;
		FontSize = Enum.FontSize.Size24;
	})
end

local function removed(playerName)
	game.StarterGui:SetCore('ChatMakeSystemMessage', {
		Text = playerName..' has left the game';
		Font = Enum.Font.SourceSansBold;
		Color = textColor.Color;
		FontSize = Enum.FontSize.Size24;
	})
end

RemoteEvent.OnClientEvent:Connect(welcome)
RemoteEvent2.OnClientEvent:Connect(removed)
4 Likes

This is one option. Another is to just have a PlayerAdded and PlayerRemoving event fire in a LocalScript. Or you can hook the chat system and have the system send a message to the “All” channel when a player leaves or joins.