I need help with a kick command

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I am trying to make a kick command.

  2. What is the issue? Instead of kicking the wanted player it kicks the player who wrote the message.

  3. What solutions have you tried so far? I have asked some friends and still couldn’t fix it.
    here is the script I have written.


game.Players.PlayerAdded:Connect(function(plr)

	plr.Chatted:Connect(function(message,recipient)
		local msg = string.lower(message)


		local PLR = Instance.new("StringValue",script)
		local sS = string.split(msg, " ")
		local sS1 = sS[1]
		local PLRNAME = PLR.Value
		if plr.Name == "Smixzol" or "CoiyoFish" or "puugoii_alt"  or "Cheeze_Noodle" or "bahakayra1318" then
			if sS[1] == "/kick" then
				if sS[2] then
					for i,txt in pairs(sS) do
						if i == 1 then
						else
							PLR.Name = sS[2]
							PLRNAME = sS[2]
							for i,wantedplr in pairs(game.Players:GetChildren()) do
								PLR.Name = wantedplr.Name
								PLRNAME = wantedplr.UserId
								print(plr.Name , "sus")

								if plr.Name ==  PLR.Name then
									print("trolled")
									wantedplr:Kick("You have been kicked")
								end
							end
						end
					end
				end
			end
		end
	end)
end)

If you could help I’d love that, thanks!

Note: From my perspective I haven’t found any errors in the output.

Read @SyntaByte’s reply to have a nice explanation of what you did wrong.

Below is also one of the many ways to make a “kick” command.

local Players = game:GetService("Players")

local Whitelist = {
	148242233,	-- k_as
	378365544	-- bahakayra1318
}

Players.PlayerAdded:Connect(function(Player)
	
	if not table.find(Whitelist, Player.UserId) then return end
	
	Player.Chatted:Connect(function(Message)
		if string.len(Message) <= 1 then return end 	-- message can't be a space or 1- character
		Message = string.split(Message, " ")
		if #Message <= 1 then return end 			-- message needs at least one space splitting arguments
		
		local Command = Message[1]
		local Recipient = Message[2]
		
		if Command == "/kick" then
			if Players:FindFirstChild(Recipient) then
				Players:FindFirstChild(Recipient):Kick("Yeet")
			end
		end
	end)
end)
1 Like

A few things to point out in your code.

This following if statement is invalid. You cannot use or like this.
if plr.Name == "Smixzol" or "CoiyoFish" or "puugoii_alt" or "Cheeze_Noodle" or "bahakayra1318" then

You need to compare plr.Name with every string. As such:

if plr.Name == "Smixzol" or plr.Name == "CoiyoFish" or plr.Name == "puugoii_alt" or plr.Name == "Cheeze_Noodle" or plr.Name == "bahakayra1318" then

Alternatively, you can simplify this by doing the following.

local Admins = {"Smixzol", "CoiyoFish", "puugoii_alt", "Cheeze_Noodle", "bahakayra1318"}

if (table.find(Admins, plr.Name) ~= nil) then
-- Kick player code here.
end

Assuming your kick command is “kick/playername”, you can do the following to retrieve the player name.

local playertokick = string.split(messsage, "/")[2]

if (playertokick ~= nil and game.Players:FindFirstChild(playertokick) then
    game.Players:FindFirstChild(playertokick):Kick("You have been kicked");
end

The final code should look like the following.

local Admins = {"Smixzol", "CoiyoFish", "puugoii_alt", "Cheeze_Noodle", "bahakayra1318"}

game.Players.PlayerAdded:Connect(function(player)
    if (table.find(Admins, player.Name) ~= nil) then -- Check admin messages only.
        player.Chatted:Connect(function(message, recipient)
            local splitmsg = string.split(messsage, "/")
            if (splitmsg[1] == "kick") then
                local playertokick = messsage[2] -- Get the target player name.

                if (playertokick ~= nil and game.Players:FindFirstChild(playertokick) then -- Check for the player and kick them.
                    game.Players:FindFirstChild(playertokick):Kick("You have been kicked");
                end
            end
        end)
    end
end)
3 Likes

this is why it kicks the player who fired the command

also you don’t really need to use for i v in pairs loop tho

you can do something like this

game.Players:FindFirstChild(PLR.Name):Kick("get kicked lol")

and this, player named “Smixzol” will be the only one who can uses this command

to fix this, do like this

if plr.Name == "Smixzol" or plr.Name == "CoiyoFish" or plr.Name == "puugoii_alt" or plr.Name == "Cheeze_Noodle" or plr.Name == "bahakayra1318" then
2 Likes

Actually, this will always result to true, which would give every player the ability to use the command. Since a string literal cannot be false.

By every player, I mean every player that joins the game.

2 Likes