This kick system's kick reason is the player's name?

I’m currently making a kick system in my game, which you type in all the info (player to kick and the reason) into textboxes. Then, you press a button and it fires a remote event on the server to kick the player in the player textbox. It works, but the reason box thing doesn’t. Everytime I test it out, it always prints the player’s name instead of the actual reason.
Here’s my code:

LocalScript:

local button = script.Parent.kickbutton
local reason = script.Parent.reasonbox
local playertext = script.Parent.playerbox

button.MouseButton1Click:Connect(function()
	if playertext.Text == "" or reason.Text == "" then
		button.Text = "oml just enter a player name and a reason how hard is it???"
		wait(3)
		button.Text = "kick cuz theyre annoying LOL"
	else
		local playertokick = playertext.Text
		local kickreason = script.Parent.reasonbox.Text
		KickEvent:FireServer(playertokick,kickreason)
	end
end)

ServerScript:

local KickEvent = game.ReplicatedStorage.KickPlayer

-- the reason is always the player's name.
KickEvent.OnServerEvent:Connect(function(playertokick,kickreason)
	playertokick:Kick(kickreason)
end)

The kick message is:

You were kicked from this game: LazokkYT

Any way to fix this?

Edit: I actually don’t know if the reason player name is the player that kicked the other player’s name or just the player that got kicked’s name.

2 Likes

The issue is that the first parameter on the .OnServerEvent is the player who fired the event. So your parameters should be in this order: player, playertokick, kickreason. I would recommend implementing sanity checks and also the playertokick argument is a string and not a player instance.

I tried putting it in the order you told me to, but it just prints
ServerScriptService.KickServerEvent:4: attempt to call a nil value

Also, I’m sure that the playertokick argument is a string because I’m “grabbing the text from the textbox”.

Because you are running the :Kick() function on a string. Use the string to retrieve the player.

I have no idea what you’re saying at this point lol.

Edit: By the way, do I have to get the player?

Yes, you HAVE to retrieve the player, you can not run :Kick() on a string.

To get the player name through a string, assuming that this string is the players entire name, you can use:

game:GetService("Players"):FindFirstChild(string)

Alternatively (and preferably), to cover when the admin is not using the player’s full username, you can use this function to determine the player:

local Players = game:GetService("Players")
function FindPlayer(name)
	name = string.lower(name)
	for _,player in pairs(Players:GetPlayers()) do
		local playerName = string.lower(player.Name)
		if string.sub(playerName,1,#name) == name then
			return player
		end
	end
end

Sorry if this is a really dumb question, but is the name argument the player that is kicking the other player’s name or the player that is getting kicked’s name?

I’m bad at scripting I’m sorry.

The name passed through the FindPlayer function would be the name of the person being kicked. Since we are using RemoteEvents we already have the player instance of the administrator, we simply need to find the player instance that needs to be kicked.

KickEvent.OnServerEvent:Connect(function(sender, playertokick, kickreason)
	if (sender.Name == "LazokkYT") then -- a sanity check is REALLY recommended for a remote event that kicks people.
		local actualplayer = game.Players:FindFirstChild(playertokick) -- since 'playertokick' is a string.
		if (actualplayer ~= nil) then
			actualplayer:Kick(kickreason)
		end
	end
end)
1 Like

I would recommend doing if actualplayer then and removing the ~= nil part.

Ok so I found out the issue, but I don’t know how to fix it. So basically, whenever I use the kickreason argument, it uses the playertokick argument instead.

Anyone know how to fix it?

Edit: It kicks the player that requested to kick the other player.

change

to

KickEvent.OnServerEvent:Connect(function(player,playertokick,kickreason)

I already tried that. It just gives me an error.

KickEvent.OnServerEvent:Connect(function(player, playertokick,kickreason)
local GamePlayer = game.Players:FindFirstChild(playertokick)
if (sender.Name == “LazokkYT”) then
GamePlayer:Kick(kickreason)
end
end)

could add a check to see if its nill but i feel thats unnecessary. This is because it would just send the sample text if nothing was written.

Isn’t that just @Blokav 's script? By the way, what is the sender variable?

No it isnt i reduced the lines of code but i did decide that i should add a sanity check. But just paste this in and it will work. @Blokav script had a nill check while this is unnecessary.

A check is necessary, what if the admin put in an invalid name? The script would just error and we do not want that.

@LazokkYT The sender parameter is the user who fired the remote.

No because there is already a client side check

there is also an else…

The problem is here:

playertokick:Kick(kickreason)

should be replaced by

game.Players:FindFirstChild(playertokick):Kick(kickreason)
1 Like