Need help with ban script

Hello, I’m making a ban system script for my game, using DataStoreService.

Ban saving works, but I’m not sure unban and time ban is working correctly, and I’m not that good at DataStores, so the script may not work properly, thanks for your response!

Server side script:

--// Services
local DSS = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")

--// Variables
local BanDataStore = DSS:GetDataStore("BanDataStore_3")
local banEvent =  RS:WaitForChild("BanEvent")
local kickEvent = RS:WaitForChild("KickEvent")

local secondsInOneDay = 86400

--// Security Code
local Code = "2SLD25637ZNAE7LL9XC8PNFVC473EEBZ4G975GU63D7CXLTJR23CXRKLXXT42745RERMB2ARCSQSTQR3YMAWXE7HS42J9L43FFJ4TEPESNXBLU5BCJXVL84QUY2ZKBSMQJHY5MWCPUF27VY792WD3N6Y7TENTTH7TWGQYCXSPP9NTNN463KHSJNALUEUG366QFCEGSTQU878DJ64SP2KLBL2H53ZJJFUCJYSM8TNQYQV3ECPJ44Z4MBCEQMM9LC8T6E4QHF63WX75R46M4HT6D3AJ99EUHPN2UKVVBXYRBTRQGLZM6Z6KMH3L33"

--// Functions
local function banPlayer(player, reason, lenght)
	if game.Players:FindFirstChild(player.Name) then 
		local success, errmsg = pcall(function()
			BanDataStore:SetAsync(player.UserId.."-ban", true)
			BanDataStore:SetAsync(player.UserId.."-reason", "You are banned for: "..lenght.." (seconds, calculated by os.time! Moderator notes: "..reason.." Wish to appeal? Join our discord server!")
			BanDataStore:SetAsync(player.UserId.."-lenght", lenght)
			BanDataStore:SetAsync(player.UserId.."-LastTimeWasOn", os.time())
		end)
	
		if success then
			print("Successfully banned "..player.Name.."!")
		else
			print("Oops, looks like something went wrong, and we can't ban"..player.Name.."! Please try again!")
		end
		player:Kick("You are banned for: "..lenght.."! Moderator notes: "..reason.." Wish to appeal? Join our discord server!")
	end
end

--// Main
game.Players.PlayerAdded:Connect(function(plr)
	local lenght
	local banned
	local reason
	local wasOn
	
	local success, errmsg = pcall(function()
		banned = BanDataStore:GetAsync(plr.UserId.."-ban")
		reason = BanDataStore:GetAsync(plr.UserId.."-reason")
		lenght = BanDataStore:GetAsync(plr.UserId.."-lenght")
		wasOn = BanDataStore:GetAsync(plr.UserId.."-LastTimeWasOn")
	end)
	
	if lenght ~= nil and wasOn ~= nil then
		if wasOn >= lenght then
			BanDataStore:RemoveAsync(plr.UserId.."-ban")
			BanDataStore:RemoveAsync(plr.UserId.."-reason")
			BanDataStore:RemoveAsync(plr.UserId.."-lenght")
			BanDataStore:RemoveAsync(plr.UserId.."-LastTimeWasOn")
		end
	end
	
	if success then
		print("Successfully loaded ban data!")
		if banned then
			if banned == true then
				plr:Kick(reason)
			end
		end
	else
		print("Something went wrong with datastore, ban data loading failed!")
	end
end)

--// Events
banEvent.OnServerEvent:Connect(function(sender, plrToBan, reason, lenght, code)
	if code == Code then
		banPlayer(plrToBan, reason, lenght)
	end
end)

And the local script in StarterGui:

local Code = "2SLD25637ZNAE7LL9XC8PNFVC473EEBZ4G975GU63D7CXLTJR23CXRKLXXT42745RERMB2ARCSQSTQR3YMAWXE7HS42J9L43FFJ4TEPESNXBLU5BCJXVL84QUY2ZKBSMQJHY5MWCPUF27VY792WD3N6Y7TENTTH7TWGQYCXSPP9NTNN463KHSJNALUEUG366QFCEGSTQU878DJ64SP2KLBL2H53ZJJFUCJYSM8TNQYQV3ECPJ44Z4MBCEQMM9LC8T6E4QHF63WX75R46M4HT6D3AJ99EUHPN2UKVVBXYRBTRQGLZM6Z6KMH3L33"
local part = game.Workspace.BanPart
local secondsInOneDay = 86400
local db = false
local days = 0.005

part.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) and db == false then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		game.ReplicatedStorage.BanEvent:FireServer(plr, "You are banned LOL!", os.time() + days * secondsInOneDay, Code)
		db = true
		wait(5)
		db = false
	end
end)

I’m planning to make ban GUI, but now I’m just testing it with part.

Note: I’m pretty new to scripting, so I found os.time() only today, and idk how to use it, but I tried to do something…

1 Like

So does it work or does it not work?

I have tested on my friend, the days value was 0.005, and os.time was, 1612786465, and it banned him to os.time 1612872702, but Seconds in a day (86400) * 0.005 will be 432, and that means it banned him for more time, also when i tried to print any value there was an error

*any value from datastore

I see no reason why unbanning wont work, however I would like to suggest something to you.

A bit of your script

This alone uses datastore 4 times, datastores do have limits so less requests is better. you can merge all the data and split it up upon loading. Along with this you have a bunch of checks that could be reduced down as it is rather unnecessary along with the fact that if data loading or saving fails it does not try again.

How I would go about it
local DSS = game:GetService("DataStoreService")
local BanStore = DSS:GetDataStore("BanData")
local BanData = nil -- Created outside of function to reduce memory clutter
local SplitVer = nil

function CreateBan(Player,Reason,Duration,BannedBy)
   BanData = Reason .. ";Split;" .. Duration .. ";Split;" .. BannedBy
   while Success == nil do
      local Success,Error = pcall(function()
         BanStore:SetAsync(tostring(Player.UserId), BanData)
      end)
      wait(3)
   end
end

function CheckForBan(Player)
    while Success == nil do
         local Success,Error = pcall(function()
           BanData = BanStore:GetAsync(tostring(Player.UserId))
         end)
         wait(3)
    end
    if BanData == nil then
    -- Player Is Not Banned
    else
       SplitVer = string.split(BanData,";Split;")
       -- Check Duration Of Ban Here, Im too lazy to write it
       Player:Kick("You Have Been Banned By ".. SplitVer[3] .. "For " .. SplitVer[1] .. "Until " .. SplitVer[2])
    end
end
2 Likes

Thanks, i will try to fix my script now, i will give solution, after i will fix, if these functions will work!

1 Like

What about Success?

 while Success == nil do

It’s underlined, do i need to create blank variable?

No, Do:

  local Success,Error = pcall(function()
      BanData = BanStore:GetAsync(tostring(Player.UserId))
 end)

if Success then
       print("Got Data!")
end

if Error then
      print("Failed to get data, error: "..Error)
end

You can also use an else statement in the if success then statement, and then print error to get the reason why it didn’t work.

I already fixed, but thanks for response!

1 Like