Kick script gives me a attempt to index nil error any help?

Hello im making a kick button and im getting this error:
01:43:47.846 ServerScriptService.KickScript:24: attempt to index nil with ‘Kick’ - Server - KickScript:24
This is the line its erroring on:

game.Players:FindFirstChild(playerToKick):Kick(reason)

This is the full script:

local admins = {
	
	"312150141",
	"180936205",
	"593325190",
	"529395500",
	"1051919117",
	"1643344166"
	
}

game.Players.PlayerAdded:Connect(function(player)
	for i, v in pairs(admins) do
		if player.UserId == v then
			game.StarterGui.AdminGui9000:Clone().Parent = player:WaitForChild("PlayerGui")
		end
	end
end)


local reason = "  You have been kicked lool!"

game.ReplicatedStorage.Kick.OnServerEvent:Connect(function(player, playerToKick, reason)
	game.Players:FindFirstChild(playerToKick):Kick(reason)
end)

Local script:

local button  =  script.Parent

local textbox = script.Parent.Parent.KickPlayerNameBox


button.MouseButton1Click:Connect(function()
	if game.Players:FindFirstChild(textbox) then
	else
		game.ReplicatedStorage.Kick:FireServer(textbox)
	end
end)

And yes i did follow a tutorial.

Also im sure this is a very easy fix also if you were wondering the tutorial i followed ment for you to have a “Reason” text box to type your reasoning since i couldnt find a tutorial that just had the kick part.

1 Like

Hopefully you are not using this in a real game that would be very insecure.

The problem is that you are not validating whether or not the player exists.

The solution would be an if statement and also you do not need Find First child in this situation either.

game.ReplicatedStorage.Kick.OnServerEvent:Connect(function(player, playerToKick, reason)
	local Player = game.Players[PlayerToKick.Name]
    if Player then -- I recommend (and table.find(ModList,Player.Name) sanity check too
        Player:Kick(Reason)
    end
end)

This is assuming you are sending the player object

2 Likes
game.ReplicatedStorage.Kick.OnServerEvent:Connect(function(player, playerToKick, reason)
	game.Players:FindFirstChild(playerToKick):Kick(reason)
end)

here I would recommend that you do an condition first to check if the player really exists for example:

if game.Players:FindFirstChild(playerToKick) then
    game.Players:FindFirstChild(playerToKick):Kick(reason)
end
1 Like

The player you are kicking doesn’t exist, check if the player exists with an if statement and check if the player that fired the remotevent is an admin like so:

for _, userId in pairs(admins) do
    if player.UserId == userId then
        if player then
            player:Kick(reason)
            break
        end
    end
end
1 Like

This is true. An exploiter could easily kick any players they want once they figure out the exploit.

1 Like

It’s easy to patch though, I’m sure OP would add security like checking the player who sent it (hence the admin list)

2 Likes

Nope! I have no idea how to make that!

It didnt work still am i suspose to put it inside the

or not

I was gonna use this in my actual game…I didnt know it was bad security…
@JamminRedPandaMan @koziahss

maybe it might have something to do in your local script. Could you please show the code for your local script (that fires the remote event). Also, i noticed you mentioned reason twice in your server script (one as a variable and another in the OnServerEvent function). You might want to remove one of them.

1 Like

Oh i thought i put the local sript in the OP guess ill have to do that heres the local script:

local button  =  script.Parent

local textbox = script.Parent.Parent.KickPlayerNameBox


button.MouseButton1Click:Connect(function()
	if game.Players:FindFirstChild(textbox) then
	else
		game.ReplicatedStorage.Kick:FireServer(textbox)
	end
end)

Try this code:

local button  =  script.Parent

local textbox = script.Parent.Parent.KickPlayerNameBox

button.Activated:Connect(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		if textbox.Text == plr.Name then
			game.ReplicatedStorage.Kick:FireServer(textbox)
		end
	end
end)
1 Like

Still gives me the error in the server script as stated in the OP
The event fires but it doesnt kick because of an error

1 Like

Ohh wait i found the issue. You put in textbox when firing the remote event, not textbox.Text.
Use the code below, Im sure it will work this time:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KickEvent = ReplicatedStorage:WaitForChild("Kick")

local button  =  script.Parent

local textbox = script.Parent.Parent:WaitForChild("KickPlayerNameBox")

button.Activated:Connect(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		if textbox.Text == plr.Name then
			KickEvent:FireServer(textbox.Text)
		end
	end
end)
1 Like

Ok now im getting this error 12:50:19.663 Players.spydercam500.PlayerGui.AdminGui9000.MainFrame.KickButton.Kick:15: Expected ‘)’ (to close ‘(’ at line 8), got - Studio - Kick:15
Also i dont think the local script was the problem i think the server script that handles the kick is…

Sorry i forgot to add the last end). I already edited the post above. Recopy the code from above and paste it into your local script

1 Like

It worked! Also as stated by @koziahss is this really a unsecure script for kicking players? Since exploiters can kick you or something

2 Likes

Im not really sure, because you already put a code in the server script that only gives the admingui to Specific players ServerSide. So i dont think exploiters can get the gui for themselves because they cannot access the server scripts, but im not 100% sure.

1 Like

Hmm ok well thanks anyways for helping!

1 Like

You’re welcome! Happy to help!

1 Like