Script can't find players name

Hello developers! I am making a new system called an admin panel.
There is a feature to kick players and it is not working…

Script:

local Players = game.Players:GetChildren()
local Player = game.Players.LocalPlayer
local KickingName = script.Parent.Parent.TextBox.Text
local Offender = game.Players:FindFirstChild(KickingName)

script.Parent.MouseButton1Click:Connect(function(plr)
	Offender:Kick("You have been kicked.")
end)

Error:

Players.TheKman2019.PlayerGui.Manager.Kick.TextButton.Script:7: attempt to index nil with 'Kick'

Thanks!

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local KickingName 
local Offender




script.Parent.MouseButton1Click:Connect(function(plr)
    KickingName = script.Parent.Parent.TextBox.Text
    Offender = Players:FindFirstChild(KickingName)
    if Offender then
         Offender:Kick("You have been kicked.")
    end
end)
``

Also, it would be better if you did the kick on the server.
1 Like

You are getting the offender before the function, do this:

local Players = game.Players:GetChildren()
local Player = game.Players.LocalPlayer
local KickingName;
local Offender;

script.Parent.MouseButton1Click:Connect(function(plr)
	KickingName = script.Parent.Parent.TextBox.Text
	Offender = game.Players:FindFirstChild(KickingName)
	if(Offender) then Offender:Kick("You have been kicked.") end;
end)

Updated with the check that @Valkyrop suggested

2 Likes

Since the Text can sometimes be a string that doesnt represent a player, you should always check if that text indeed represents a player.

Works but for some reason, doesn’t read the name of the player that the person types into the textbox.

In the current script you have, you would have to type the name exactly, case sensitive.

If you want to not have to type case sensitive, you have to loop through all players and string.lower() both the name and the player you are comparing it to. Like so:

local playerName = Offender:lower()
for _, player in pairs(game.Players:GetPlayers()) do
	if player.Name:lower() == playerName then
		player:Kick("message here")
		break
	end
end

Also this should be done on the server side, so have the button fire a RemoteEvent to the server.

Also prevent the game creator from being kicked, and prevent exploiters from using the remote event by checking permissions on the server side.

3 Likes