Kicking a player with a command

Hello! I am currently stuck on making a ban command once an admin messages it. And i can’t seem to make it work! If any of you could help me i would appreciate it.

Script in workspace:

local module = require(game.ReplicatedStorage.mods)
local bt
local admins = module.admins

game.Players.PlayerAdded:Connect(function(plr)
	local plrss = game.Players:GetChildren()
	for e,q in pairs(plrss) do
	for i,v in pairs(admins) do
		game.Players[v].Chatted:Connect(function(msg)
			if string.find(msg, "!ban") then
				bt = string.sub(msg, 12,13)
				game.Players[string.find(msg, q.Name)]:Kick("                                                               You have been banned by - "..v.."                                                                                             Ban time: "..bt)
			end
		end)
		end
		end
end)

ModuleScript in ReplicatedStorage:

local module = {}

module.admins = {
	"Dro1z", "Player2", "Player1"
}

return module
2 Likes
-- player.chatted bla bla bla
local split = msg:split(" ")
if split[1] == "!ban" then
local player = split[2]
game.Players:FindFirstChild(player):Kick(split[3] or nil)
end
3 Likes

Like what @Qinrir mentioned, you can use string.split() which returns a table of the message. From this, we can just index the table to get each segment of the text.

And for the check of whenever the player is an admin, we can just use table.find() to check if the player’s name is in the given admin table instead of looping through it with a generic for loop.

local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")

local mod = require(RS.mods)
local admins = mod.admins

PS.PlayerAdded:Connect(function(player: Player)
	if not table.find(admins, player.Name) then
		return
	end
	
	player.Chatted:Connect(function(msg: string)
		local split = string.split(msg, " ")
		
		if split[1] == "!ban" then
			local cmd: string = split[1]
			local target: Player = PS:FindFirstChild(split[2])
			local reason: string = split[3]
			
			if target then
				target:Kick(reason or nil)
			else
				warn("player not found!")
			end
		end
	end)
end)