Chat Commands script not kicking target?

I’m revamping my chat commands and I want it to kick whatever target is provided. I’m using string.gmatch() + string.find() for this. (Haven’t used string.find much) To simplify the script, I also decided to use module scripts, which are handling some of the functions such as returning the closest match to a string etc. However, although my script is correctly identifying my permission as an admin, and detecting what command I’m using (evidence based on print-debugging), it fails to kick the target i provide. Help would be much appreciated!

Module Script (With All IDs and functions):

local mainTable = { --Table to store all functions, permited Players and valid commands
	["permittedIDs"] = { ---Table to store IDs of all 
		366734064 --My User ID
	},
	["validCmds"] = { --Table to store all cmds, for checks
		"J:Kick"
		
	}

} 

--Functions--
function mainTable.FindClosestMatchString(typeofsearch, placeToSearch, String) --Search for closest match
	String = string.lower(String) -- make it comparable
	if typeofsearch == "PlayerSearch" then
		if placeToSearch ~= game.Players:GetPlayers() then
			placeToSearch = game.Players:GetPlayers() ---Not sure if neccessary, maybe if i make a mistake 
		end
		for indexPlayer, Player in pairs(placeToSearch) do --pairs loop
			if string.find(string.lower(Player.Name), String) then --return match
				return Player
			end
		end
	elseif typeofsearch == "CommandSearch" then
		local CMDtable = placeToSearch
		for indexCommand, Command in pairs(CMDtable) do -- pairs loop
			if string.find(string.lower(Command), String) then
				return Command --return command
			end
		end
	end
end

function mainTable.BreakUpStringIntoSpaces(String) --String.gmatch method
	local Table = {}
	for word in string.gmatch(String, "%a+") do -- %a+ is the string pattern to split up into spaces
		table.insert(Table, #Table +1, word)
	end
	return Table --return table full of broken up string
end
return mainTable --Make it accessible to scripts

Server script (Handling the .Chatted event):

local plrService = game:GetService("Players")
local MainModule = require(game.ServerStorage.MainModule)
local allowed = false
function checkPerm(plr)
	for indexID, ID in pairs(MainModule.permittedIDs) do
		if plr.UserId == ID then
			return true
		end
	end
end
plrService.PlayerAdded:Connect(function(plr)
	allowed = checkPerm(plr)
	plr.Chatted:Connect(function(stringMsg)
		if allowed then
			print(plr.Name.." Is Allowed")
			local ArgsTable = MainModule.BreakUpStringIntoSpaces(stringMsg)
			local Command = ArgsTable[1]
			pcall(function()
				Command = MainModule.FindClosestMatchString("CommandSearch", MainModule.validCmds, Command)
			end)
			if Command == "J:Kick" then
				print("Kicking...")
				local Target = ArgsTable[2]
				if Target ~= nil then
					if string.lower(Target) == "all" then
						for indexPlayer, Player in pairs(game.Players:GetPlayers()) do
							if Player == plr then
								print("Will not kick")
							else
								Player:Kick(ArgsTable[3])
							end
						end
					else
						local Found_Player = MainModule.FindClosestMatchString("PlayerSearch", game.Players:GetPlayers(), Target)
						if Found_Player ~= nil then
							Found_Player:Kick(ArgsTable[3])
						end
					end
				end
			end
		else
			print(plr.Name.." Is Not Allowed")
		end
	end)
end)

1 Like

Is it properly breaking up your string? Everything looks fine to me with the actual kicking script. Try breaking it up and seeing if it’s detecting names properly. Additionally, I’m a little confused as to why you search using GetPlayers and then use your module to do so.

1 Like

Yeah, looking back at that now, I shouldn’t of repeated using :GetPlayers() in module and server script, I’ll debug the script a little and see if it’s breaking it down properly by looping through the table it returns

Hmm… it seems it does break it down. But i think it isn’t doing it properly.
E.g.
I chatted:

J:Kick All Ok

and the table storing the string printed this:

J
Kick
All 
Ok

I think the issue is, because the second index in this table stores ‘Kick’, it is unable to get the target, as the target is instead being stored in the third index.

For some reason it splits the command line, which isn’t what I wanted. I want it to be like so:

J:Kick
All
Ok

Instead. Is the string pattern i use affecting the results?

EDIT:
I fixed my issue by making the target variable store the 3rd index. However, I would still appreciate if there was a way I could achieve what i said before hand about not splitting the prefix/command

1 Like