Ban local script not working

Hello,
I am trying to make a ban system but only in a local script.
The script is not working for some reason.

local bandata = {}

game.Players.PlayerAdded:Connect(function(plr)
local data = table.find(bandata, plr.Name)
if data[1] == plr.Name then
plr:Kick("You are banned for: "..data[2])
end
end)
table.insert(bandata,game.Players["usernamhere"].Name,"reasonhere")
game.Players["usernamhere"]:Kick()
1 Like

You should probably not use a Client sided script for a ban script. Instead, use a server one in ServerScriptService.

Heres how this could work:

local Bans = {"Cedestius"}
local Players = game:GetService("Players")

function playerBanned(plr)
   for i,v in pairs(Bans) do
      if plr.Name == v then
      plr:Kick("You are currently banned from this game.")
      break
   end
end

Players.PlayerAdded:Connect(playerBanned)

Add this in a Server-Sided script in ServerScriptService, if you are triyng to do a Manual Ban. If you want an admin system ban, you would have to use datastores to execute that.

Without providing any specific errors or output it will be hard for anyone to give you any sort of specific details as to why your script is not working.

From what I can tell you are attempting to kick someone from the game if their name matches what is in the bandata table, and then attempting to index data[2] to find their ban reason (you have the right idea but this isn’t quite right.)

Just a disclaimer as well: you should not be handling bans from the client as they can be circumvented by exploiters relatively easily. I’ll be providing a snippet of how I would approach this.

-- BanServer.lua

local Players = game:GetService("Players")

local BanData = {
  [12345678] = "Banned for cheating." -- Banning via UserId is up to preference, but is it really a ban if you're basing it off usernames that can be changed?
}

Players.PlayerAdded:Connect(function(Player)
   local Ban = BanData[Player.UserId]
   
   if Ban then 
        Player:Kick(Ban)
   end
end)
3 Likes

^Use this guys solution, Its better!!!

3 Likes

How would you get the BanData reason? and how would you table.insert a new ban

I have an open source system you can use, if you want (Simple BanGUI - Roblox).
Also, having a ban system only on the client is impossible. Your client can only kick itself, and also can’t use Datastoreservice to handle saving bans.