How to make a ban system/command (tutorial)

I know there may have been posts regarding how to make a ban system but I wanted to make it easier for the community to find. This tutorial will cover how to make a very basic ban system I will show you how to make a perma ban (permanent ban) system/command.

Tutorial has been updated to follow suggestions made by @colbert2677

So to get started open the world you want to add this to and insert a script in ServerScriptService
I’ll call the script “Ban” in the script we will set up a A few service variables first:

local DS = game:GetService("DataStoreService"):GetDataStore("BanDataStore")
local Players = game:GetService("Players")

Next we will add a player added function and get the player from it

local DS = game:GetService("DataStoreService"):GetDataStore("BanDataStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
    -- Ban Code Will Go Here
end)

After That we will want to get the key we will be using for the data store and check if there is a value saved in the datastore using a pcall function:

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.use rid
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key) -- Get Values From DataStore
	end)
end)

Next we want to check if the pcall was successful with retrieving the data, if so we will want to check if the stored value is true, if so then kick the player and tell them they are banned from the game!

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
    end
end)

Next we will add an else statement to give the default value (false) if no value was found in the data store

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
end)

Alright we are done with the datastore, now we will make the way the player will get banned (which will be through an admin command /ban plrName )

So what you will want to do is check if they (the person who joined) are an admin, make sure you use their userId instead of their username as their username can change at anytime unlike their userId. For me it will look like this (as I will be the only admin I only need to check if the player is me by using my player id)

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
	
	if player.userId == 154609412 then

    end
end)

If they are an admin we will setup a chatted event that gets the message they said:

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
	
	if player.userId == 154609412 then
	   player.Chatted:Connect(function(msg)

       end)
    end
end)

After that we will want to see if “/ban” is in the message:

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
	
	if player.userId == 154609412 then
		player.Chatted:Connect(function(msg)
			if msg:match("/ban")  then

          end
       end)
    end
end)

If it is we will want to split the string (to get the player to kicks name as the command will be called using “/ban playersName”), Check if the player is a valid player and then if they are then set their saved ban value to true and kick them.

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
	
	if player.userId == 154609412 then
		player.Chatted:Connect(function(msg)
			if msg:match("/ban")  then
				local plrToBanName = msg:split(' ')[2]
				local plrToBan
				if game.Players:FindFirstChild(plrToBanName) then
					plrToBan = game.Players:FindFirstChild(plrToBanName)
					DS:SetAsync("id_"..plrToBan.userId, true)
					plrToBan:Kick("You have been banned from this game!")
				end
			end
		end)
	end
end)

If the username they typed does not belong to a valid player then we will warn them

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
	
	if player.userId == 154609412 then
		player.Chatted:Connect(function(msg)
			if msg:match("/ban")  then
				local plrToBanName = msg:split(' ')[2]
				local plrToBan
				if game.Players:FindFirstChild(plrToBanName) then
					plrToBan = game.Players:FindFirstChild(plrToBanName)
					DS:SetAsync("id_"..plrToBan.userId, true)
					plrToBan:Kick("You have been banned from this game!")
				else
					warn(plrToBanName, " IS NOT A VALID PLAYER")
				end
			end
		end)
	end
end)

So thats it for the tutorial hope you enjoyed and hoped this helped you out below is the final code:

local DS = game:GetService("DataStoreService"):GetDataStore("BanDataStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local plr_key = "id_"..player.userId
	local banSave = false
	
	local success, res = pcall(function()
		return DS:GetAsync(plr_key)
	end)
	if success then
		if res then
			player:Kick("You are banned from this game!")
		end
	else
		DS:GetAsync(plr_key, false)
	end
	
	if player.userId == 154609412 then
		player.Chatted:Connect(function(msg)
			if msg:match("/ban")  then
				local plrToBanName = msg:split(' ')[2]
				local plrToBan
				if game.Players:FindFirstChild(plrToBanName) then
					plrToBan = game.Players:FindFirstChild(plrToBanName)
					DS:SetAsync("id_"..plrToBan.userId, true)
					plrToBan:Kick("You have been banned from this game!")
				else
					warn(plrToBanName, " IS NOT A VALID PLAYER")
				end
			end
		end)
	end
end)
8 Likes

You should move this to #resources:community-tutorials.

4 Likes

It was there but got removed for some reason and they never responded to my question

1 Like

How could i unban the player then?

1 Like
DS:SetAsync("id_"..plrToBan.UserId, false)
1 Like

Sorry I didn’t see this, do what @Minhseu123 said, for unban just set the ban to false

How do I make a specfic time period for this script so it doesn’t perma ban?

It will have a few extra features, I will write up a new tutorial when I get to my computer, then I will link it to you.

Ok thanks!

char limit

You can store the time that the user was banned at (os.time; refered to as banTime later on in this post), as well as the time (in seconds) that they should be banned for (refered to as TimeToBeBanned…). When they join, if ```os.time > banTime + TimeToBeBanned```` then kick em again

Thanks for helping me!

Can you give me a script that does this since I don’t know how to implement this.