Kick Command is not working: What have I done wrong?

For my game King of the Hill, I am working on an admin system, but I have run into an issue.

Whilst making the :kick command, I realised that the string cannot be identified with an Array Key.

Here is the script in full:

local Plrs = game:GetService("Players")
local Commands = {}
Commands.__Index = Commands
local LinkedCommands = {[':kick']=Commands.Kick}
function Commands.Kick(argument:string)
	if string.split(argument,' ')[1] == argument then
		local CurrentPlrs = Plrs:GetPlayers()
		local PossiblePlayer
		for i, v in pairs(CurrentPlrs) do
			local PlrNameSection = string.sub(v.Name,1,#argument)
			if PlrNameSection == argument then
				PossiblePlayer = v
			end
		end
		if PossiblePlayer then
			PossiblePlayer:Kick()
		end
	end
end

Plrs.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(message)

		local Start = string.split(message,' ')[1]
		if LinkedCommands[Start] then
			print("should've sent...")
			local CmdN = LinkedCommands[Start]
			local Arguement = string.sub(message,#Start+2,#message)
			local Cmd = LinkedCommands[CmdN](Arguement)
			
		end
	end)
end)

The variable LinkedCommands hosts what word at the start of the sentance is needed, and links it to a command function, like Commands.Kick.

Currently I have no permission or arguement checking, but I know how to implent that and that is not a priority as of now.

When I type :kick c, it should kick me since my 2017 username starts with ‘c’.

It kicks a player by checking which player’s starting characters are the same as the arguement, however, the issue is not with the :kick command, it is with the linking process.

Even when the chat detects my first word as :kick, it returns nil as it can’t find a key in LinkedCommands called :kick, but there is a value assigned to that key, so I have absalutely no idea what the issue is.

1 Like

So I have made commands and I would suggest using this.

1 Like

Thanks for your suggestion, but I am not asking for a resource for command support, I am asking on what the issue with my one is.

1 Like
local Plrs = game:GetService("Players")
local Commands = {}
Commands.__Index = Commands
local LinkedCommands = {[':kick']=Commands.Kick}
function Commands.Kick(argument:string)
	if string.split(argument,' ')[1] == argument then
		local PossiblePlayer = game.Players:FindFirstChild(argument)
		if PossiblePlayer then
			PossiblePlayer:Kick()
		end
	end
end

Plrs.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(message)

		local Start = string.split(message,' ')[1]
		print("sending...")
		if LinkedCommands[Start] then
			print("should've sent...")
			local CmdN = LinkedCommands[Start]
			local Arguement = string.sub(message,#Start+2,#message)
			local Cmd = LinkedCommands[CmdN](Arguement)
			
		end
	end)
end)

I hope this works also are there any errors with the original script and this script? if so please inform me

1 Like

First of all, you changed the kick command that was not the issue and makes the command require the entire username. Secondly you just added a print in the one with the issue, which isn’t going to solve anything.

I appreciate that you tried to help but that is not what I need.

I know that it goes through the line with local Start = ..., so no need for the print.

And this does not solve what I was pointing out where the issue was; linking strings to functions from the LinkedCommands variable.

1 Like

yes i added the print to see that if the print shows which it does

Yes, but that is not my issue, I know that it will always pass that point.

1 Like

well i know why it errors its cause the command doesn’t exist for some reason i am currently seeing why. I suggest using my link as its really simple to understand

It does not error.

It just does not send if it does not find a key with the same name as the Start variable.

The issue is that LinkedCommands[':kick'] should return a command, but returns nil as it can’t find the key :kick in the array.

yes exactly

The command does exist, the key does exist.

The problem is that the first word of the message and ':kick' aren’t equal, which is very strange, since both of them are strings and both have the same bytedata.

It is issue with identifying the key, not that the command does not exist.

this is kinda dumb but try using double quotes (") instead of single quotes (’)

Nevermind, I was wrong. They’re both used for strings in Lua. My bad, thanks for pointing this out to me.

I tried doing that and it does not make a difference.

Also in Lua " and ' are treated the same, so in any circumstance that can’t be an issue.

Both are considered string, no difference.

I even did print(Start==:kick) and it always printed true when I said something in chat begining with :kick, but it does not get the key with the exact same name.

Can you try to print the possible players?

Can you tell what’s the error?

Code I copied from yours + the prints
local message = ":kick c"
local Start = string.split(message,' ')[1]
print(string.sub(message,#Start+2,#message))
print(string.format("\"%s\"", Start))

You’re assigning the kick command before you even create it, Commands.Kick should be made before making LinkedCommands. For example:

local Commands = {}
function Commands.Kick(argument:string)
	if string.split(argument,' ')[1] == argument then
		local CurrentPlrs = Plrs:GetPlayers()
		local PossiblePlayer
		for i, v in pairs(CurrentPlrs) do
			local PlrNameSection = string.sub(v.Name,1,#argument)
			if PlrNameSection == argument then
				PossiblePlayer = v
			end
		end
		if PossiblePlayer then
			PossiblePlayer:Kick()
		end
	end
end
--Since Commands.Kick exists now, we can assign it.
local LinkedCommands = {[':kick']=Commands.Kick}
2 Likes

I would do that when it goes to the function, but the thing is that it does not run the commands.Kick, because it des not link to it.

Thanks! I applied that + a minor change and now it works.

When I do type in chat: ':kick c' or :'kick coolalex183', it would kick me, showing that Command.Kick has no issue.

Here is the result:

local Plrs = game:GetService("Players")
local Commands = {}
Commands.__Index = Commands
function Commands.Kick(argument:string)
	if string.split(argument,' ')[1] == argument then
		local CurrentPlrs = Plrs:GetPlayers()
		local PossiblePlayer
		for i, v in pairs(CurrentPlrs) do
			local PlrNameSection = string.sub(v.Name,1,#argument)
			if PlrNameSection == argument then
				PossiblePlayer = v
			end
		end
		if PossiblePlayer then
			PossiblePlayer:Kick()
		end
	end
end

local LinkedCommands = {[':kick']=Commands.Kick}

Plrs.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(message)

		local Start = string.split(message,' ')[1]
		if LinkedCommands[tostring(Start)] then
			print("should've sent...")
			local Arguement = string.sub(message,#Start+2,#message)
			local Cmd = LinkedCommands[Start](Arguement)
			
		end
	end)
end)

Time for me to work on kick reasons & permission checking!

Thanks for your help!