Need help with chat commands

Hi devs,
I need some help with my chat commands. Right now for a kick command am doing this:

game.Players.LocalPlayer.Chatted:Connect(function(realMessage)
	local message = string.lower(realMessage)
	if string.sub(message, 1, 6) == ":kick " then
		local player = string.sub(message, 7)
		if game.Players[player] then
			game.ReplicatedStorage.KickPlayer:FireServer(player)
		else
		end
	end
end)

It works fine but I want to add a reason thingy in it. I mean if I do :kick anirudh851 test then it should kick me with the reason “test”. I have already achieved the reason in a gui, I do player:Kick(reason) but here I can’t find the index where the reason starts. I know that the player name starts at 7 index as the command :kick is fixed and it doesn’t change, but player names have dynamic length, some can be small and others can be big. I just want to know the index where the reason starts. Hope you have understood what I mean, sorry my english is bad. Thanks.

The player’s name will always be lowercase in the message variable. But what if the player’s name isn’t lowercase? I recommend you use string.sub(realMessage, 7) instead.

but then if i type :KICK anirudh851 it wont work

Also here’s an improvement to your script:

local chosenPlayer = nil
local function getPlayer(name)
  for i, v in pairs(game.Players:GetPlayers()) do
    if string.sub(v.name:lower(), 1, #name) == name:lower() then
      chosenPlayer = v
    end
  end
  if not chosenPlayer then return nil end
end
game.Players.LocalPlayer.Chatted:Connect(function(realMessage)
	local message = string.split(realMessage, ' ')
	if message[1]:lower() == ":kick" then
		local player = getPlayer(message[2])
        table.remove(message, 1); table.remove(message, 2)
        local result = message:concat(' ')
        if not player then return end
		if game.Players[player] then
			game.ReplicatedStorage.KickPlayer:FireServer(player, reason)
		else
		end
	end
end)
1 Like

I have been instructed to do the script like that. Otherwise I know what you have added, thanks. But it didn’t answer what I made this topic for.

Oh yeah I forgot, let me fix that.

1 Like

I edited it, you should add that extra parameter which will be the reason

Thanks. I’ll get a look at the script and then I’ll add it. Will test and reply back if any problems occur.

It also works when you use partial names, for example: :kick anirudh this is the reason

1 Like

Instead of using string.sub to determine if you are using the kick command, you could split the message by spaces.
Also, Player.Chatted also works on the server, this would be better for security.

local Players = game:GetService("Players")
local admins = {--[[names here]])

local function onPlayerAdded(player)
    local function onChatted(message)
        if not table.find(admins, player.Name) then return end

        local args = message:split(" ")
        if args[1] == ":kick" and #args >= 2 then
            local target = getPlayer(args[2])
            if target then
                target:Kick("You have been kicked by "..player.Name)
            end
        end
    end
    player.Chatted:Connect(onChatted)
end

Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

just a second, you never declared the reason var before and never set its value either. what will be the value of the reason var? sorry, am just a new scripter.

Isn’t Player.Chatted recommended in a Script? Alongside, since you’re splitting a message with " " (space), if message[1]:lower() == ":kick " then would check for the space at the end.

I didn’t think about that, you can change the reason variable to this: local result = message:concat(' ') or 'default reason here'

1 Like

I was forced to put it on the client cuz i need to fire a remote event to the server to kick the player

You don’t need a RemoteEvent to kick a player - instead, it can be done manually in the Script.

You could split the message by spaces and combine the later words

Something like

local reason
if #args >= 3 then
    reason = table.concat(args, " ", 3)
else
    reason = "No reason provided."
end
1 Like

Oh yeah I forgot about that, I was editing his script

I am using a separate script on sss that gets the re and kicks the player. and i forget i can do that here also

I got it to work. Thanks @GoteeSign!

1 Like

No problem! If anything is broken just tell me the problem and i’ll fix it

1 Like