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)