Custom Kick message with Player.Chatted

Hey again! I’m making a custom player.Chatted ban command but I want to know how I can add a custom kick message when kicking the player. For example: lua "ban PlayerName Exploiting, do not exploit. and the kick message is lua "You have been banned. Reason: Exploiting, do not exploit. The problem is I don’t know how to do it with what I have so far. Here is the script:


local Owner = { OwnerID = 996906122}
local prefix = ";"





game.Players.PlayerAdded:Connect(function(plr)
	if plr.UserId == Owner.OwnerID then
		print("Owner "..plr.Name.." has joined the game")
		plr.Chatted:Connect(function(Message)
			if Message:sub(1,5) == prefix.."ban "  then
				local Target = game.Players:FindFirstChild(Message:sub(6))
				
					if not Target then
						
				 print("target not found") 
				else
						Target:Kick("You have been banned. Reason: "..KickMessage)
			      
			   end
		    end
		end)
	end
end)
2 Likes

I would suggest that you split the message using string.split() and for the separator use a white-space. This will give you the command, name fragment, and the rest of the message will be the reason. You should sub the message by using the combined lenght of the name fragment and command with the spaces, which will give you the command reason.

Made a function so it’s easier to get the player from the message

local PlayerService = game:GetService("Players")

local Owner = { OwnerID = 996906122 } -- Change to your ID
local prefix = ";"

local function GetPlayerFromArg(argument)
	for index , person in ipairs(PlayerService:GetPlayers()) do
		if string.match(person.Name:sub(1,argument:len()),argument) then
			return person
		else
			warn("Player not found!")
		end
	end
end

PlayerService.PlayerAdded:Connect(function(plr)
	print("Owner " .. plr.Name .. " has joined the game!")
	
	plr.Chatted:Connect(function(Message)
		
		if plr.UserId == Owner.OwnerID then
			
			if string.lower(Message:sub(1,4)) == prefix .. "ban" then
				local commandSplits = string.split(Message:sub(6,-1)," ")			
				local targetPlayerFromArg = GetPlayerFromArg(commandSplits[1])
				local commandReason = Message:sub((7 + commandSplits[1]:len()),-1)
				
				print(targetPlayerFromArg.Name , commandReason)
				
				targetPlayerFromArg:Kick(commandReason)			
			end
			
		end
		
	end)
	
end)

Using Chatted, it’s smart to split the message into separate arguments. You can use string.split() with a " " delimiter to turn a string into a table, where each element in the table is an individual word from the message that comes from Chatted.
This will allow you to individually process each argument e.g.

local message = ";ban TheParkourGamer10 Being a really cool developer."

Using string.split(), we can split this message by spaces and pack it into a table

local args = string.split(message, " ")
for _, v in pairs(args) do
    print(v)
end

=>

;ban
TheParkourGamer10
Being
a
etc

With this, we can now assign things like the command being used, the player that’s being interacted with, and a reason.

local command = args[1] -- The first element (word) in the message
local player = args[2] -- Second argument in the message
local reason = ....

The reason part might be a little tougher, since it’s multiple words. It’s important to realize the reason argument goes from args[3] all the way to the end of args. We can use table.concat() to join these together to form an entire string

local reason = table.concat(args, " ", 3)

Now we have three parameters, the command used, the player and the entire reason to give the player.
There are several ways to get the player instance from the player string, I’ll leave that to you for now. And it seems you already have the whole Reason portion understood for the Kick function.
Hope this helps!

3 Likes
--//Kick Command\\--
game.Players.PlayerAdded:Connect(function(Player)
	
	local admin = " " --UserName here
	
	-- Kick


	Player.Chatted:Connect(function(Message)
		local SplitMessage = Message:split(" ")
		if SplitMessage[1] == "!kick" and Player.Name == admin then
			local NameOfPlayerToWarn = SplitMessage[2]
			local PlayerToWarn = game.Players:FindFirstChild(NameOfPlayerToWarn)
			local Reason = Message:split(NameOfPlayerToWarn)[2]
			
			PlayerToWarn:Kick("Reason:"..Reason)
		end
	end)
end)