Kicking Script Issues

Hello Developers,

I am having trouble with this kicking Gui. I have an autofill script for a name input. If I have no text in the name input it kicks me no matter what but if I choose a different player it kicks them. I don’t understand why it’s kicking me though? Anyone know why or how this is happening? Or how to fix it?

Client Script

local function getPlayerFromPartialName(PartialName)
	local foundName = nil
	local Players = game.Players:GetPlayers()
	for i = 1, #Players do
		local PossiblePlayer = Players[i]
		if string.find(string.lower(PossiblePlayer.Name), string.lower(PartialName)) then
			foundName = PossiblePlayer.Name
		end
	end
	
	if not foundName then
		return nil
	else
		return foundName
	end
end

script.Parent.ScrollingFrame:WaitForChild("Kick").Activated:Connect(function()
	local KickingName = getPlayerFromPartialName(script.Parent.Autofill.Text)
	game.StarterGui:SetCore("ChatMakeSystemMessage", { 
		Text = KickingName.." has been kicked from the session.",
		Font = Enum.Font.GothamSemibold,
		FontSize = Enum.FontSize.Size96,
	})
	print(script.Parent.Autofill.Text)
	print(KickingName)
	game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("KickEvent"):FireServer(KickingName)
end)

Server Script

local function getPlayerFromPartialName(PartialName)
	local foundName = nil
	local Players = game.Players:GetPlayers()
	for i = 1, #Players do
		local PossiblePlayer = Players[i]
		if string.find(string.lower(PossiblePlayer.Name), string.lower(PartialName)) then
			foundName = PossiblePlayer.Name
		end
	end
	
	if not foundName then
		return nil
	else
		return foundName
	end
end

KickEvent.OnServerEvent:Connect(function(player, KickingName)
	local PlayerToKick = getPlayerFromPartialName(KickingName)
	print(player.Name.." is Kicking "..PlayerToKick)
	game.Players:FindFirstChild(PlayerToKick):Kick("You have been kicked from the game.")
end)

What shows in Output? I see you have multiple “print()” functions.

It all prints saying it’s going to kick me but my name is not mentioned anywhere.

Is the text already your username?
Or is the text blank?

With my experience, TextBoxes aren’t very functional, I recommend like a list of all players, and you can click any one of them to kick them.

That is a good idea. I just find textboxes more simple. But no, the text is blank.

I have to go now, DM me on Discord: Row453#8598 if you have found a fix.

Why not just check if the length of ‘PlayerToKick’ is 0, if so, just return out of the function.

No need to do all that, Perhaps the player is nil, If so do a pcall()

    game.Players[PlayerToKick]:Kick("You have been kicked from the game.")

No need to do all that FindFirstChild and stuff…

   local success, err = pcall(function()
        game.Players[PlayerToKick]:Kick("U have been kicked from this game")
   end)

    if success then
        print("Succesfully kicked player.")
    end

   if err then
       print("Could not kick player, Something went wrong.")
   end

And where do you call the function getPlayerFromPartialName?, There is no need to do all that if nil checking, When I made my system it bypassed the nil checking and still printed out the error (Player is nil) , Once you have added all you’re changes I guess that should work for ya.

You are printing who fired the remote event. (The player variable in the ServerScript).

And for your function to autofill in the client, your Player object is always first in a LocalScript. So that’s why it kicks you. You mish check the length of PlayerToKick variable, as EdibleGames said.