Kick command doesn't actually kick for the reason defined?

I have a kick command like so:

CommandsTable.kick = function(...)
	local args = {...}
	for _, v in pairs(args) do
		print(v )
	end
	print("Getting the targeted player and the reason!")
	local TargetedPlayer = args[1][2]
	print(TargetedPlayer)
	local foundPlayer = FunctionsTable.FindClosestMatch(TargetedPlayer)
	if foundPlayer then
		local reason
		for i = 3, #args do
			reason = reason.." "..args[1][i]
		end
		foundPlayer:Kick(reason)
	else
		warn("Couldn't find player "..TargetedPlayer)
		return
	end
end

It identifies the player but it is unable to kick the player for the specified reason. I tried something different by allowing unlimited parameters, so maybe my new exposure to such things is the main reason why I am failing.

Server Script:

local ReturnedTable = Config.Functions.BreakupMessage(Message)
			for _, SplitPart in pairs(ReturnedTable) do
				print(SplitPart)
			end
			local RequestedCommand = string.lower(Prefix..ReturnedTable[1])
			print(Player.Name.." said: "..ReturnedTable[1])
			local CommandFound, Result  = pcall(function()
				CommandTable[string.lower(ReturnedTable[1])](ReturnedTable)
			end)
			if CommandFound then
				print("Found Command")
			else
				warn(Result)
			end

There are no errors, for the server script code I realise it is malformed but I simply just copy and pasted part of the code, anything else is irrelavant.

Thanks!

Btw the output:

**Server - ChatHandlingScript:17
  17:05:41.777   ▼  {
                    [1] = "kick",
                    [2] = "really",
                    [3] = "being",
                    [4] = "irresponsible",
                    [5] = "good",
                    [6] = "riddance",
                    [7] = "to",
                    [8] = "bad",
                    [9] = "nonsense"
                 }  -  Server - Config:35
  17:05:41.779  Getting the targeted player and the reason!  -  Server - Config:37
  17:05:41.780  really  -  Server - Config:39
  17:05:41.783  Found Command  -  Server - ChatHandlingScript:22
  17:05:41.787  Server Kick Message:   -  Studio ** --Should be "being irresponsible good riddance to bad nonsense" lol kinda random tho
1 Like

I doubt this would fix your issue but in the case of wanting to stitch back together a variable number of arguments into something like a kick message I’d use table.concat. You can start 3 elements in and then traverse the rest of the table’s length.

if foundPlayer then
    local reason = table.concat(args, " ", 3, #args)
    foundPlayer:Kick(reason)
end

Some repro code:

local args = {"kick", "really", "being", "irresponsible", "good", "riddance", "to", "bad", "nonsense"}
print(table.concat(args, " ", 3, #args))
2 Likes

Doing that actually worked! Thank you so much, I’ll keep that in mind for later usage. Btw I changed the code up a bit too with this contribution to make it work

1 Like