Hello! So, I’m creating a kick GUI but it doesn’t seem to work. Any help?
LocalScript
local KickUser = script.Parent.KickBox.Text
local KickText = script.Parent.KickReason.Text
local KickReason = ''
local plr = game.Players.LocalPlayer
--//Kick System
script.Parent.KickButton.Activated:Connect(function()
if KickText == '' then
KickReason = '\n\nReason: Reason UnSpecified\nKicked By: '..plr.Name
else
KickReason = '\n\nYou Were Kicked From The Game!\nReason: '..KickText..'\nKicked By: '..plr.Name
end
if game.Players:FindFirstChild(KickUser) then
script.Parent.Kick:FireServer(KickUser, KickReason)
else
script.Parent.KickBox.Text = 'Invalid Player!'
end
end)
game.Players(user) would be the syntax to call a function called Players from the game object. To actually get the user, you need to use [square brackets] not (parentheses), so you would do game.Players[user]:Kick(reason).
ServerScript :
local plr
game:GetService(“ReplicatedStorage”).Kick.OnServerEvent:Connect(function(p, user, reason)
print(user)
for _,v in pairs(game.Players:GetPlayers()) do
if v.Name == user then
plr = v
else
print(“Player not found!”)
end
end
plr:Kick(reason)
end)
I formatted the code so it is easier to read and understand.
LocalScript:
local KickUser
local KickText
local KickReason = ''
local plr = game.Players.LocalPlayer
script.Parent.KickButton.Activated:Connect(function()
KickUser = script.Parent.plr.Text
KickText = script.Parent.reason.Text
if KickText == '' then
KickReason = '\n\nReason: Reason UnSpecified\nKicked By: ' .. plr.Name
else
KickReason = '\n\nYou Were Kicked From The Game!\nReason: ' .. KickText .. '\nKicked By: ' .. plr.Name
end
game:GetService("ReplicatedStorage").Kick:FireServer(KickUser, KickReason)
end)
ServerScript:
game:GetService("ReplicatedStorage").Kick.OnServerEvent:Connect(function(p, user, reason)
for _, player in pairs(game.Players:GetPlayers()) do
if player.Name == user then
player:Kick(reason)
else
print("Player not found!")
end
end
end)
This code still has a flaw that was mentioned by @SS4PPHIRE. You should consider adding an admin table on the server and compare that to the player UserId who sent the RemoteEvent. It would be better if you just put more of this code on the server instead of leaving the server doing the simple tasks.
The problem isn’t where the script is located on the server, it is that any player can fire the RemoteEvent to kick another player because you aren’t checking whether the player that fired the RemoteEvent is an admin / has permission to do so. This leaves it open for any exploiter to kick anyone in the game because you haven’t stopped unauthorised players from kicking.
They can still fire it even if it’s in a GUI, and they are able to find out the parameters using a RemoteSpy or something. All they would have to do to kick everyone from the server is something like this:
for i,v in pairs(game.Players:GetPlayers()) do
remote:FireServer(v.Name, "get kicked noob")
end
Luckily, adding fixes to prevent this is extremely simple. You could just add a :GetRankInGroup() function if you wanted to keep it simple, or have a permissions table or something.
game:GetService("ReplicatedStorage").Kick.OnServerEvent:Connect(function(p, user, reason)
if p:GetRankInGroup(urgroupid) >= rankid then
for _, player in pairs(game.Players:GetPlayers()) do
if player.Name == user then
player:Kick(reason)
else
print("Player not found!")
end
end
else
p:Kick("exploiter")
end
end)
Edit: The only thing you would want to be careful about is that you dont give the UI to any staff who aren’t allowed to use it to avoid kicking people for exploiting when they just don’t have the proper permissions to do it.
local banList ={12213, 24124, 124124} -- these are UserIds of banned people
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
if table.find(banList, player.UserId) then
player:Kick(reason)
end)
Why did you use remote events? You can just do this on PlayerAdded and see whether a player’s UserId is in the array, if it is then kick the player