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.
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.
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?
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)
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.
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.
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.