For Some Reason When I try to kick a player with my chat command it never returns a target. I'm not sure if this is an issue linked to my "SearchForClosestMatch" function or something else. I would be great if someone could point me in the right direction and show where I messed up. Help Appreciated. Thanks!


Code:


local plrService = game:GetService("Players")
local plrsPermitted = require(script.plsPermitted)
local validCmds = require(script.validCmds)
function CheckPlayer(plr, module)
	local IsMatching = false
	for _, validplr in pairs(module) do
		if plr.Name == validplr then
			IsMatching = true
		end
	end
	return IsMatching
end
function validCmd_Check(cmdstring, module)
	for _, validCmd in pairs(module) do
		if string.lower(cmdstring) or string.upper(cmdstring) == string.lower(validCmd) or string.upper(validCmd) then
			return validCmd
		end
	end
end
function SearchForClosestMatch(String, CurrentTableOfPlayers)
	for _, plr in pairs(CurrentTableOfPlayers) do
		if string.match(string.lower(plr.Name), "^"..string.lower(String)) then
			return plr
		else
			return nil
		end
	end
end

plrService.PlayerAdded:Connect(function(plr)
	local argsTable = {}
	local IsAllowed = false
	if CheckPlayer(plr, plrsPermitted) then
		IsAllowed = not IsAllowed
	end
	plr.Chatted:Connect(function(stringMsg)
		if IsAllowed then
		for word in string.gmatch(stringMsg, "%a+") do
			table.insert(argsTable, #argsTable+1, word)
		end
		if #argsTable >= 0 then
				local cmd = validCmds[1]
				if validCmd_Check(cmd, validCmds) ~= nil then
					local actualCmd = validCmd_Check(cmd, validCmds)
					if actualCmd == "J:Kick" then
						local Target = argsTable[2]
						local PlayerTarget = SearchForClosestMatch(Target, plrService:GetPlayers())
						local Reason = argsTable[3]
						PlayerTarget:Kick(Reason)
					end
				end
			end
		end
	end)
end)

P.S. I am using string.gmatch in this script, and I'm pretty new to it. Please tell me if I'm using it wrong.

1 comment

0

Also I am aware that using name verification is insecure, I am working on using IDs