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)