An efficient and simple ban system

In this tutorial im going to show you how to make an efficient ban system using a datastore and some string manipulation

by Zolyphony

Author Avatar

Roblox does not automatically give you a system for banning players that may cause trouble in your games. This tutorial is going to go over an efficient and simple way to ban a player

The first thing we need to do is set up a data store inside of a server script. We can do this by using the datastore service

##API services MUST be enabled inside of your game settings security area

Step 1: Setting up the datastore

	local DB = game:GetService("DataStoreService")
	local datastore = DB:GetDataStore("Stats")

These two lines of codes get the service and initalize a datastore for us to use throughout the script. We are then going to make a function for banning a player

Step 2: The ban function

	local DB = game:GetService("DataStoreService")
	local datastore = DB:GetDataStore("Stats")

	local function BanPlayer(ID)
		local suc,err = pcall(function()
			datastore:SetAsync(ID,true)
		end)
		if err then
			print(err)
		end
	end

We start the function by getting the Players ID which is going to be used as the key/identifier for the data and we then use a Protected Call (A protected call is a way to use functions without ending the script if an error occurs inside, ideal for datastores)

SetAsync() sets a value to a key which in this case the key is the players ID and the value of the key is if they're banned or not

Step 3: Checking if a player is banned

Now we need to use the PlayerAdded event to read this data (This is still inside the server script)

game.Players.PlayerAdded:Connect(function(player)
	local ID = player.UserId
	local suc,err = pcall(function()
		local data = datastore:GetAsync(ID)
		if data == true then
			player:Kick("You are banned!")
		end
	end)
	if err then
		print(err)
	end
end)

GetAsync() is used to find the data behind a key inside of a datastore, which in this case is the UserId as that is unique to everyone. The data is stored as a boolean of true or false and if it is equal to true, the player will be kicked from the game.

Step 4: Checking if a player is an admin

The data changing is now in place. But what about activating the function? How about we do it where an administrator uses a command.

local admins = { } --add your user id or any admins user ID in this table as an integer

game.Players.PlayerAdded:Connect(function(player)
	local ID = player.UserId
	local suc,err = pcall(function()
		local data = datastore:GetAsync(ID)
		if data == true then
			player:Kick("You are banned!")
		end
	end)
	if err then
    	print(err)
    end
	for _,adminID in admins do
		if ID == adminID then
			player.Chatted:Connect(function(message)

			end)
		end
	end
end)

With this we are now able to check if a player is an admin by searching a table for a matching userID meaning the script will only fire the Chatted event for players that are admins

Step 6: Banning the player

local admins = {} --add your user id or any admins user ID in this table as an integer

game.Players.PlayerAdded:Connect(function(player)
	local ID = player.UserId
	local suc,err = pcall(function()
		local data = datastore:GetAsync(ID)
		if data == true then
			player:Kick("You are banned!")
		end
	end)
	if err then
    	print(err)
    end
	for _,adminID in admins do
		if ID == adminID then
			player.Chatted:Connect(function(message)
				local newMessage = message:split(" ")
				if newMessage[1] == "/ban" then
					local banned = newMessage[2]
					local bannedID = game.Players:GetUserIdFromNameAsync(banned)
					BanPlayer(bannedID)
					if game.Players:FindFirstChild(banned) then
						game.Players[banned]:Kick("You have been banned")
					end
				end
			end)
		end
	end
end)

Lots of new stuff with this change!

Introducing the split function! Split allows us to manipulate a string into segments depending on when the applied character appears. For example: If i used split like string:split(" ") and string = "/ban Zolyphony" string[1] would be "/ban" and string[2] would be "Zolyphony".

We then get the players user id by using :GetUserIdFromNameAsync() incase the player being banned is offline. we then call the BanPlayer() function we wrote earlier and send through the ID.

Lastly. We check if the player is in game and if they are they get kicked while being informed that they have been banned.

And just like that you have a working ban system!

##Compare your script to the full script here incase you have any errors

local DB = game:GetService("DataStoreService")
local datastore = DB:GetDataStore("Stats")

local function BanPlayer(ID)
	local suc,err = pcall(function()
		datastore:SetAsync(ID,true)
	end)
	if err then
		print(err)
	end
end

local admins = {} --add your user id or any admins user ID in this table as an integer

game.Players.PlayerAdded:Connect(function(player)
	local ID = player.UserId
	local suc,err = pcall(function()
		local data = datastore:GetAsync(ID)
		if data == true then
			player:Kick("You are banned!")
		end
	end)
	if err then
    	print(err)
    end
	for _,adminID in admins do
		if ID == adminID then
			player.Chatted:Connect(function(message)
				local newMessage = message:split(" ")
				if newMessage[1] == "/ban" then
					local banned = newMessage[2]
					local bannedID = game.Players:GetUserIdFromNameAsync(banned)
					BanPlayer(bannedID)
					if game.Players:FindFirstChild(banned) then
						game.Players[banned]:Kick("You have been banned")
					end
				end
			end)
		end
	end
end)

View in-game to comment, award, and more!