So I am creating an admin UI were you can kick, perm ban and unban someone. Below is what I have done but the issue I am having is it will only let me perm ban myself not others. Not sure why though.
(Script to ban - Server Script):
local DataStoreService = game:GetService("DataStoreService")
local banDataStore = DataStoreService:GetDataStore("banDataStore")
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = player.UserId
local banned
local success, errormessage = pcall(function()
banned = banDataStore:GetAsync(playerUserId)
end)
if banned == true then
player:Kick("You are banned from this game!")
end
end)
game.ReplicatedStorage.BanSystem.OnServerEvent:Connect(function(plr, userToBan, reason)
local plrUserID = userToBan.UserId
local success, errormessage = pcall(function()
banDataStore:SetAsync(plrUserID, true)
userToBan:Kick("You are banned!")
game.Players:FindFirstChild(userToBan):kick(reason)
end)
local playerUserId = plr.UserId
local success, errormessage = pcall(function()
banDataStore:SetAsync(playerUserId, true)
game.Players:FindFirstChild(userToBan):kick(reason)
end)
end)
game.ReplicatedStorage.UnbanSystem.OnServerEvent:Connect(function(plr, userToBan, reason)
local playerUserId = plr.UserId
local success, errormessage = pcall(function()
banDataStore:SetAsync(playerUserId, false)
end)
end)
You are not actually checking if userToBan is an actual player or referring to their player instance when banning them. Id use something like this to get the player when you fire your remote event:
local PlayerToBan = game.Players:FindFirstChild(userToBan) -- I assume that userToBan is a string which you have sent from the client
plrUserId = PlayerToBan.UserId
This code will ban yourself because you are the “plr” variable. Also exploiters could fire this remote and ban people. On the server side, make a table will “Admin” player UserIds, and before banning/unbanning on the server, check if the player who sent the remote event has a UserId that matches the table.
Yes technically. You could make the ban system use their name instead. The problem with that is if they change their name then it won’t save their ban. Now most exploit accounts are alt accounts, and to change your name you need 1000 robux and I doubt that an alt account would have that many roux so that should be fine.
When you ban someone, you could put their UserId in a datastore and have their Username as the key. Then when you want to unban someone you can get their UserId out of the datastore using their name.