I can't get string.split to work

I am trying to make a kick admin command but I want the player to be able to set a kick reason instead of a pre-set reason.

I am using string.split but I’m not sure if I am doing something wrong or not.

local admins = {1234}
local prefix = ":"

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		for i, id in pairs(admins) do
			if player.UserId == id then
				if msg:lower() == prefix.."kick" then
					local player = game.Players:FindFirstChild(msg:sub(7))
					local splitMessage = string.split(msg, " ")
					player:Kick(splitMessage[3])
				end
			end
		end
	end)
end)

(1234 was changed to my user id so that wasn’t the problem) I have searched on the wiki and dev forums but it didn’t seem to help.

msg is the whole message and you are checking if it is equal to “:kick”. I think you want to check if the first 5 characters are equal to “:kick” as right now the only valid message it’ll take is literally “:kick”.

Hm, it works but only if I just do the username, it doesn’t work with a kick reason.

That is because msg:sub(7) contains the reason as well, since you aren’t separating it by space as in splitMessage. Here is a working solution.

local admins = {53500920}
local prefix = ":"

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		for i, id in pairs(admins) do
			if player.UserId == id then
				if msg:lower():sub(1, 5) == prefix.."kick" then
					local args = msg:sub(7):split(" ");
					local player = game.Players:FindFirstChild(args[1]);
					
					if (player) then
						player:Kick(table.concat(args, " ", 2));
					end
				end
			end
		end
	end)
end)
1 Like

Thanks a lot, I should have realised that :grinning:

1 Like