That checks if the player entered something or not. The nil check checks if there is a player under that name.
OP should check that a player exists with the name they entered. Otherwise you are just assuming that they exist and the script could error if there is no such player.
Sorry for the late reply, but it just errors
ServerScriptService.KickServerEvent:3: attempt to index nil with 'Kick'
in the output.
Could you show me your entire code now?
Hi, I would suggest something a little more secure as this could easily be spoofed by exploiters but I’ll still help you out with your current code.
Your LocalScript code is completely okay and I do not see any issues with it.
In your ServerScript I’d suggest going for something like this:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Event
local KickEvent = ReplicatedStorage:WaitForChild("KickEvent", 2)
assert(KickEvent, "Unable to locate the KickEvent")
-- the reason is always the player's name.
KickEvent.OnServerEvent:Connect(function(LocalPlayer, To_Kick, Reason_For_Kick)
for _, Player in next, Players:GetPlayers() do
if Player.Name:lower():match("^" .. To_Kick:lower()) then -- Provided starts with any player's first name
Player:Kick(Reason_For_Kick)
print(LocalPlayer.Name .. " has kicked: " .. Player.Name)
return
end
end
end)
You should probably make the player name lowercase as well.
Thank you for pointing that out, I was kind of in a hurry so I didn’t notice.
Just remember to do some kind of sanity check, because right now anyone who knows how to fire remote events could kick any player in the game with this.
I’m actually letting everyone kick each other lol. I’m gonna add a debounce and something to check you aren’t kicking the same person over and over again.
Here is how the server script should look:
local KickEvent = game.ReplicatedStorage:WaitForChild('KickPlayer')
local admins = {
['LazokkYT'] = true
}
KickEvent.OnServerEvent:Connect(function(player, Target, reason)
print('Event')
if admins[player.Name] then
print('Admin')
if game.Players:FindFirstChild(Target) and reason ~= nil then
local LockedTarget = game.Players[Target]
print('Kick')
LockedTarget:Kick(reason)
print('Kicked')
else
print('Not a valid Player Name/Reason')
end
else
player:Kick('Attempting to fire the Admin Kick Event')
end
end)
I also added an admin table with your name so that exploiters can’t kick people.
I Tested this and it works, so It would be nice if you clicked the “Solution” on my comment.
Thank you!
This should be the server script:
local event = game.ReplicatedStorage.KickPlayer
event.OnServerEvent:Connect(function(player, playertokick, reason)
local PlrToKick = game.Players:FindFirstChild(playertokick)
if PlrToKick then
PlrToKick:Kick(reason)
end
end)
Hope this helps!
u shouldn’t be using remote events to kick other players at all
if a exploiter runned this script ur game would be ruined and die
local remote = game.ReplicatedStorage.KickPlayer
local me = game.Players.LocalPlayer
while wait(1) do
local players = game.Players:GetPlayers()
for _,plr in ipairs(players) do
if plr ~= me then
remote:FireServer(plr, "this server is mine now")
end
end
end
heres a thread that talks about it and tells u how to use it correctly and avoid headaches in the future