Kick command doesn't work, give error

I was trying to make a Kick command but for some reason it doesn’t work.

image

player.Chatted:Connect(function(msg)
	if string.find(msg, Prefix.."kick") then
		local args = string.split(msg, " ")
		args[2]:Kick()
	end
end)

That’s because you’re running the :Kick() method on a string. (Which is now nil)
What you need to do is to check if the Message contains a second argument, And if the Argument is inside the Players service. (Basically check if the Name matches the name of a certain Player)

Try changing your code to this and let me know if it works.

-- Variables
local Players = game:GetService("Players")
local Prefix = "!"

-- Events:
Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		-- Get the Arguments by splitting them by whitespaces.
		local Arguments = string.split(Message, " ")
		local ArgumentCount = #Arguments

		-- Do we have more than 1 argument? Yes? Proceed.
		if ArgumentCount > 1 then
			-- Is the command !kick?
			if Arguments[1] == Prefix.."kick" then
				-- Did we found the possible Player?
				if Players:FindFirstChild(Arguments[2]) then
					-- Kick the Player.
					Players:FindFirstChild(Arguments[2]):Kick()
				end
			end
		end
	end)
end)
2 Likes

You need to first look it up in the players to be able to kick it like this example

local Players = game:GetService("Players")
local function KickPlayer(Player) --Example of a Kick Function for all script for reduce script lines 
	local PossiblePlayer = Players:FindFirstChild(Player)
	if PossiblePlayer then -- if found player then 
		print("Player found!") 
		PossiblePlayer:Kick("Kicked from the game")  --Here i am kicking the player
	else
		warn("Player is not on the game") -- If doesnt found the player to kick
	end
end
--Your example fixed i believe
player.Chatted:Connect(function(msg)
	if string.find(msg, Prefix.."kick") then
		local args = string.split(msg, " ")
		KickPlayer(args[2])
	end
end)
-- Your example in other way
player.Chatted:Connect(function(msg)
	if string.find(msg, Prefix.."kick") then
		local args = string.split(msg, " ")
		local PossiblePlayer = Players:FindFirstChild(args[2])
		if PossiblePlayer then -- if found player then 
			print("Player found!") 
			PossiblePlayer:Kick("Kicked from the game")  --Here i am kicking the player
		else
			warn("Player is not on the game") -- If doesnt found the player to kick
		end
	end
end)
1 Like

Thank you for taking your time to help me! Really grateful :sweat_smile:
Hope you have a nice day

1 Like