Need some help sending a system message when the player gets kicked

I’m trying to make a script that kicks the player and posts a system message in the chat

--Variables

local player = game.Players.LocalPlayer
local canKick = {"TheXEternalXSlayer"}

--Actual script
game.Players.PlayerAdded:Connect(function(plr)
	for _, c in pairs(canKick) do
		if plr.Name == c then
			plr.Chatted:Connect(function(msg)
				if string.sub(msg,1,5) == "/kick" then
					local pl = game.Players:FindFirstChild(string.sub(msg,7))
					if pl then
						pl:Kick("RAC banned from secure server") --You can edit this message to what you prefer.
						game.StarterGui:SetCore("ChatMakeSystemMessage", {
							Text = "Player" ..player.Name.. "left the game [RAC banned from secure server]";
							Color = Color3.fromRGB(255, 151, 151);
							Font = Enum.Font.ArialBold;
							FontSize = Enum.FontSize.Size18;
						})
					end
				end
			end)
		end
	end
end)

The script kicks the player but the system message doesn’t send. what am I doing wrong?

I’ve tried switching the message before the kick but that just makes neither of them work. I can’t think of any other way to fix the script.

StarterGui:SetCore('ChatMakeSystemMessage',{
	Text = "Hello, this is a test.";
	Font = Enum.Font.GothamBold;
	Color = Color3.fromRGB(0,255,190);
	FontSize = Enum.FontSize.Size96	
})

This worked for me, so maybe try changing it from double quotes to single quotes?

you were doing this on the client, it will only affect that player

I was bored so here’s a server script using chat service runner thingy

Summary
local font = "ArialBold"
local chatColor = Color3.fromRGB(255, 85, 127)
local botName = "system"
local nameColor = Color3.fromRGB(255, 255, 255)

local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

if not ChatService:GetChannel("All") then
	while true do
		local ChannelName = ChatService.ChannelAdded:Wait()
		if ChannelName == "All" then
			break
		end
	end
end

local bot = ChatService:AddSpeaker(botName)
bot:JoinChannel("All")

bot:SetExtraData("ChatColor", chatColor)
bot:SetExtraData("NameColor", nameColor)
bot:SetExtraData("Font", font)
bot:SetExtraData("FontSize", 18)

local function Message(msg)
	wait(1)
	bot:SayMessage(tostring(msg),"All")
end
local canKick = {"TheXEternalXSlayer"}
game.Players.PlayerAdded:Connect(function(plr)
	for _, c in pairs(canKick) do
		if plr.Name == c then
			plr.Chatted:Connect(function(msg)
				if string.sub(msg,1,5) == "/kick" then
					local pl = game.Players:FindFirstChild(string.sub(msg,7))
					if pl then
						pl:Kick("RAC banned from secure server") --You can edit this message to what you prefer.
						Message("Player [" ..plr.Name.. "] left the game [RAC banned from secure server]")
					end
				end
			end)
		end
	end
end)

Hey, make a remote event and use the FireAllClients feature. In a local script, make your system message coming from the remote event

Its best if you use a local script for this situation. You want to fire a remote event to all clients when a player is kicked, just like what @TotallyDrew said above.
Follow the steps below:

Insert a remote event in ReplicatedStorage, and rename it to KickMessage. In your server script (that you have given above), you want to remove the code which makes the system message, and instead fires the remote event to all clients.
Updated Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MessageEvent = ReplicatedStorage:WaitForChild("KickMessage")

local player = game.Players.LocalPlayer
local canKick = {"TheXEternalXSlayer"}

game.Players.PlayerAdded:Connect(function(plr)
	for _, c in pairs(canKick) do
		if plr.Name == c then
			plr.Chatted:Connect(function(msg)
				if string.sub(msg,1,5) == "/kick" then
					local pl = game.Players:FindFirstChild(string.sub(msg,7))
					if pl then
						pl:Kick("RAC banned from secure server")
						MessageEvent:FireAllClients(pl)
					end
				end
			end)
		end
	end
end)

Then insert a Local Script in StarterGui, and use the following code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MessageEvent = ReplicatedStorage:WaitForChild("KickMessage")

MessageEvent.OnClientEvent:Connect(function(KickedPlr)
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "Player" ..KickedPlr.Name.. "left the game [RAC banned from secure server]";
		Color = Color3.fromRGB(255, 151, 151);
		Font = Enum.Font.ArialBold;
		FontSize = Enum.FontSize.Size18;
	})
end)

Done

2 Likes

Sorry for the late reply but this still doesn’t work. I followed all the steps you provided but still had the same issue where the Kick seems to work but the system message does not.

EDIT - Turns out I forgot a letter in the code which caused it not to work, it works perfectly now! Thanks for the help.

1 Like

No problem! Really happy to help!