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