How to make a Ban System for your game

This tutorial will include the use of a ModuleScript

  1. In this first part you will need to insert a normal script into the ServerScriptService then inside that you will need a ModuleScript. (Name them anything but make sure to call them by that Name)

image

ModuleScript:

  1. Inside the ModuleScript, name the Module (The part that says BannedUsers. Do this for return) then separate the Curly Brackett like shown below:
--- In this ModuleScript should only require the Players userId and The reason for the ban

local BannedUsers = {

}
return BannedUsers

  1. Then inside the Curly Brackets type the code below: (Read the comments that in the code to understand the use)
--- In this ModuleScript should only require the Players userId and The reason for the ban

local BannedUsers = {
	["User 1"] = {
		["User"] = "USERID HERE", --- The Players UserId
		["Reason"] = "REASON HERE" --- The reason for the ban
		
	}

}
return BannedUsers

Script:

  1. Inside the script type this code below:
local BannedUsers = require(script.ConfigUsers) -- To get the info for the ban

With the purpose of the ModuleScript it makes it easier for you to access the Names and Reasons for their ban without taking too much room up in this Script.

  1. Before coding the rest you need to make a function for when the player joins the game.
local BannedUsers = require(script.ConfigUsers) -- To get the info for the ban

game.Players.PlayerAdded:Connect(function(Player)

end)
  1. In this step we are using Loops to access the listed player. (See step 2 to understand the next part)
local BannedUsers = require(script.ConfigUsers) -- To get the info for the ban

game.Players.PlayerAdded:Connect(function(Player)
	for _, BannedPlayer in pairs(BannedUsers) do -- Getting the Player and the Reason for their ban

end)
  1. Inside the loop should be the code that kicks the player for being banned from the game.
local BannedUsers = require(script.ConfigUsers) -- To get the info for the ban

game.Players.PlayerAdded:Connect(function(Player)
	for _, BannedPlayer in pairs(BannedUsers) do -- Getting the Player and their Reason for the ban
		task.wait()
		Player:Kick(BannedPlayer.User .."(".. Player.Name ..") Reason for ban: " ..BannedPlayer.Reason) -- Kicks the player for being banned
	end
end)

BannedPlayer.User - This gets the userId that was mention in step 2.
BannedPlayer.Reason - This gets the Reason for the players ban mention in step 2.

The line:

Player:Kick(BannedPlayer.User .."(".. Player.Name ..") Reason for ban: " ..BannedPlayer.Reason) -- Kicks the player for being banned

Inside Kick() can be option for what you say when the player is kicked.

Module Script
Final Code:

--In this ModuleScript should only require the Players userId and The reason for the ban

local BannedUsers = {
	["User 1"] = {
		["User"] = "USERID HERE", --- The Players UserId
		["Reason"] = "REASON HERE" --- The reason for the ban
		
	}

}
return BannedUsers

Script
Final Code:

local BannedUsers = require(script.ConfigUsers) -- To get the info for the ban

game.Players.PlayerAdded:Connect(function(Player)
	for _, BannedPlayer in pairs(BannedUsers) do -- Getting the Player and their Reason for the ban
		task.wait()
		Player:Kick(BannedPlayer.User .."(".. Player.Name ..") Reason for ban: " ..BannedPlayer.Reason) -- Kicks the player for being banned
	end
end)

If you want to add more users then follow this example:

--In this ModuleScript should only require the Players userId and The reason for the ban

local BannedUsers = {
	["User 1"] = {
		["User"] = "USERID HERE", --- The Players UserId
		["Reason"] = "REASON HERE" --- The reason for the ban
		
	},
	["User 2"] = {
		["User"] = "USERID HERE", --- The Players UserId
		["Reason"] = "REASON HERE" --- The reason for the ban

	}

}
return BannedUsers

If you want to learn, Don’t copy and paste :slight_smile:

8 Likes

If something is wrong please comment.

This is a good tutorial for beginners however I suggest you using task.wait since its a improved version & more accurate than wait

Been Updated thanks for the suggestion :slight_smile:

1 Like

Also thank you!
I have been trying to improve on my posts

1 Like

I might be wrong but wouldn’t this just kick any player that joins the game? There’s no conditional statements in the PlayerAdded and you’ve connected the kick to the player that joins

2 Likes

This is a much better improvement than your last tutorial :slight_smile:. Great job!

The only issue I have is that I don’t find it efficient to have a manual ban list though since I have to go into Studio everytime I want to add a specific person to my ban list.

1 Like

I am working on an Admin script so i might make a tutorial with that.

1 Like

But thats what you want isn’t is? So that the player doesn’t Join

Also I would do
game.Players.PlayerAdded:Connect(function(Player)
local bannedPlayer = BannedUsers[tostring(Player)] – I just like using tostring don’t ask why lol
if bannedPlayer ~= nil then
Player:Kick(bannedPlayer.User…"("…Player.Name…") Reason for ban: "…bannedPlayer.Reason – Better than having to loop through possibly a long ban dictionary
end
end

and no only the listed players in step 2.

No what I mean is it kicks any player that joins, not those exclusive to the ban dictionary

I might consider using this next time :slight_smile:

1 Like

Oh wait mb I shouldn’t have said no to make this confusing. Yes you don’t want the player to join but as there’s nothing inside the connection saying “kick the player only if he’s in this list”, it will kick any player that joins the game.

With your current script you can quickly fix this by just doing (inside the for i, v in pairs):
if BannedPlayer.User == Player.Name then
Player:Kick(–Input reason)
return
end

1 Like

Only just realized i should of used this sorry.

With my last tutorial i have been careful with the layout of this tutorial.

1 Like

I hate to self plug, however… This whole system would be a bit easier if you were to use my ban module, EZban.

2 Likes

more efficient system

modulescript

return {
  ["commitblue"] = "because you suck"
} -- you can also use IDS

normal script

local list = require(path.to.module)
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
    local retrieved = list[plr.Name] or list[plr.UserId] or nil
    if retrieved ~= nil then
        plr:Kick(retrieved)
    end
end)

also please, this is a nice tutorial as it shows u modules unlike other ban tutorials, but can you make more ‘useful’ tutorials next time? it just makes #resources:community-tutorials no value(no offence please)

1 Like

correct me if i’m wrong but don’t you need to publish your game every time you want to ban/unban somebody? if so that could cause a lot of problems. for example if you’re working on an update but you want to ban/unban somebody, you would have to publish your game, and you know how that will turn out. i know some developers make separate games to work on updates and just put everything onto the actual game once everything is finished, but you get the point still.

but again, i’m not really sure if this is true or not because i don’t use modules often. also i don’t really know what’s considered a “good ban system”

Sure i can try make useful ones

1 Like