Temporary Ban from the game

Hi
how do I make a temporary ban from the game.
I already made a data store ban system by using _G

9 Likes

Do you mean temporarily from the server or temporarily from the whole game?

Yes, temp banned from the whole game.

If you expect your server where the player was banned from is to be up (not shut down due to lack of players) until the ban runs out you could something similar to this: Time Kick

But just make the wait time longer than 30 seconds.
This would make it so that when the player joins the will get kicked until it changes the datastore back to 0.

Use datastores, insert the os.time into the table including the player info, to know the expiry, (TIMESTAMP). And when a player joins check if the timestamp has passed.

1 Like

os.time() is the amount of seconds that has passed from 1970, 1st january. So you can use that to check the player left amount of time.

For example, when you ban a player, you multiply the period of time (DAYS) by 86400, since a day has 86400 seconds. Then you add it to the current timestamp and save it, the next time the player joins, the server will go through the datastore and check if os.time() is bigger or equal than the time saved in the datastore.

2 Likes

This isn’t as hard as you think to make. All you need to know is how to use DataStores and os.time(). I have made a quick example of a way that this could be achieved but this example is no where near the best way to do this but it should give you the basic idea.

What is os.time()?
Well, os.time() is essentially a stop watch that is continuously counting up. This means each time it is called the number will be a little bit bigger. For example if you do print(os.time()) and then call it one second later the number would have increased by one. If you wish to read more about os.time() here is a little article on the Roblox Developers hub: os | Documentation - Roblox Creator Hub

Example of how to make a temp ban system:
First you will need to set up your DataStore.

local DataStoreService = game:GetService("DataStoreService")
local TempBanStore = DataStoreService:GetDataStore("TempBanStore",2)

Second you will have to set up your variables for how long the temp bans are going to last.

local Times = {
	One_Day = 86400; -- Seconds in a day
	Three_Days = 259200;  -- Seconds in 3 days
	One_Week = 604800;  -- Seconds in 1 week
}

Now then, once we have the variables set up we can now check if a player is temp banned each time they join the game. In the code below, the players data loads and then it checks if the player is still temp banned or not.

-- Checking if player is still banned when they join.
game.Players.PlayerAdded:Connect(function(Player)
	local Success, Result = pcall(function() -- It is good practice to wrap your datastore requests in pcalls
		return TempBanStore:GetAsync(tostring(Player.UserId),"TempBan") -- Returns the players temp ban data for later use.
	end)
	
	if Success then -- Checks if the DataStore request was successful 
		if Result then -- Checks if there is any data under the key
			if Result.BanStartTime + Result.BanTime > os.time() then -- This is the part that checks if the player is temp banned or not
				print("Player's temp ban is lifted")
			else
				Player:Kick("You are temp-banned")  -- Kicks the player if they are still temp banned
			end
		end
	end
end)

We now have a way of checking if the player is temp banned or not but we don’t have a way of giving a user a temp ban. This code below should fire once a remote event is passed from the client to the server, I haven’t done this though as this is just an example.

-- Setting a ban

local function SetBan(Player,BanTime)
	local Success,Error = pcall(function()
		if BanTime == "one_day" then
			TempBanStore:SetAsync(tostring(Player.UserId),{BanStartTime = os.time(), BanTime = Times.One_Day})
		elseif BanTime == "three_days" then
			TempBanStore:SetAsync(tostring(Player.UserId),{BanStartTime = os.time(), BanTime = Times.Three_Days})
		elseif BanTime == "one_week" then
			TempBanStore:SetAsync(tostring(Player.UserId),{BanStartTime = os.time(), BanTime = Times.One_Week})
		end
	end)
	if not Success then
		print("Not successfull")
	end
end

Full code:

local DataStoreService = game:GetService("DataStoreService")
local TempBanStore = DataStoreService:GetDataStore("TempBanStore",1)

local Times = {
	One_Day = 86400; -- Seconds in a day
	Three_Days = 259200;  -- Seconds in 3 days
	One_Week = 604800;  -- Seconds in 1 week
}

game.Players.PlayerAdded:Connect(function(Player)
	local Success, Result = pcall(function()
		return TempBanStore:GetAsync(tostring(Player.UserId),"TempBan")
	end)
	
	if Success then
		if Result then
			if Result.BanStartTime + Result.BanTime > os.time() then
				print("Player's temp ban is lifted")
			else
				Player:Kick("You are temp-banned")
			end
		end
	end
end)

-- Setting a ban

local function SetBan(Player,BanTime) 
	local Success,Error = pcall(function()
		if BanTime == "one_day" then
			TempBanStore:SetAsync(tostring(Player.UserId),{BanStartTime = os.time(), BanTime = Times.One_Day})
		elseif BanTime == "three_days" then
			TempBanStore:SetAsync(tostring(Player.UserId),{BanStartTime = os.time(), BanTime = Times.Three_Days})
		elseif BanTime == "one_week" then
			TempBanStore:SetAsync(tostring(Player.UserId),{BanStartTime = os.time(), BanTime = Times.One_Week})
		end
	end)
	if not Success then
		print("Not successfull")
	end
end

Side note:
All the code above should be ran from the server for security reasons.

30 Likes

What about perm ban, and unban thingy.
And also, how do i made like in the reason it puts a unban date.

You should remove the Player’s ban from the datastore after it is over. Can cause some unwanted problems in the future.

1 Like

Already got it, But right now a issue.

image
The error.

if Result.BanStartTime + Result.BanTime > os.time() then  --that line

BanTime is a nil value because you require that value from a table, Try to simply use the number in the setBan function.

If that doesn’t help message in here again.

1 Like

Because you use _G, The script that references it does not know what Times is, Therefor I will suggest you to replace the stuff like Times.One_Day to 86400.

1 Like

Try this one.

_G.Ban = function(plr, reason, bantime)
	local Event = game.ReplicatedStorage.RemoteEvents.SystemMessage
	local Success, Error = pcall(function()
		plr:Kick(reason)
		if bantime == "one_day" then
			tempData:SetAsync(tostring(plr.UserId), {BanStartTime = os.time(), bantime = 86400}, reason)
			Event:FireAllClients(plr, "DAY-BANNED")
		elseif bantime == "three_days" then
			tempData:SetAsync(tostring(plr.UserId), {BanStartTime = os.time(), bantime = 259200}, reason)
			Event:FireAllClients(plr, "3-DAY-BANNED")
		elseif bantime == "one_week" then
			tempData:SetAsync(tostring(plr.UserId), {BanStartTime = os.time(), bantime = 604800}, reason)
			Event:FireAllClients(plr, "WEEK-BANNED")
		elseif bantime == "pernament" then
			tempData:SetAsync(tostring(plr.UserId), {BanStartTime = os.time(), bantime = 9999999999999999999999999}, reason)
			Event:FireAllClients(plr, "PERM-BANNED")
		end
	end)
	if not Success then
		print("Not successfull")
	end
end
1 Like

It is caused by the SetBan function that does not provide a valid BanTime

1 Like

17:57:47.680 - ServerScriptService.Banner:21: attempt to perform arithmetic on field ‘BanTime’ (a nil value)

I did everything, but got same error.

game.Players.PlayerAdded:Connect(function(plr)
	local Success, Result = pcall(function() -- It is good practice to wrap your datastore requests in pcalls
		return tempData:GetAsync(tostring(plr.UserId), "TempBan", reason) -- Returns the players temp ban data for later use.
	end)
	
	if Success then -- Checks if the DataStore request was successful 
		if Result then -- Checks if there is any data under the key
			local BST = Result.BanStartTime
			print(BST)
			if BST + Result.BanTime > os.time() then -- This is the part that checks if the player is temp banned or not
				print("Player's temp ban is lifted")
			else
				local status = tempData:GetAsync(tostring(plr.UserId), "TempBan", reason)
				if status then
					plr:Kick("You are banned until: "..Result.BanTime.." Reason: "..status)
				end
			end
		end
	end
end)

Just a classic case of caps issues. You need in line 19 of the main script to change BanTime to bantime. Sorry for that little mistake :sweat_smile:.

2 Likes

Thank you everything working :grin:

You should do

if Result.BanStartTime + Result.BanTime < os.time() then
1 Like

Hello! I’m getting the same error on my version. Please read the code carefully (I tried getting help nicely but they didn’t read carefully and left)

(Server Handler)

local BanData = game:GetService("DataStoreService"):GetDataStore("BanData")
--local ModerationHistoryDS = game:GetService("DataStoreService"):GetDataStore("ModerationHistory")

local HandlerRemote = script.Parent.HandlerRemote

local Times = {
	["OneDay"] = 86400;
	["ThreeDays"] = 259200;
	["OneWeek"] = 604800;
	["TwoWeeks"] = 1209600;
	["Infinite"] = 999999999999999999999999999999999999999999999999999999999999999999999999;
}

function SetBan(Player, BanTime, BanType)
	local Success, Error = pcall(function()
		if BanType == "TempBan" then
			if BanTime == "OneDay" then
				BanData:SetAsync((Player), {BanStartTime = os.time(), BanTime = Times.OneDay})
			elseif BanTime == "ThreeDays" then
				BanData:SetAsync((Player), {BanStartTime = os.time(), BanTime = Times.ThreeDays})
			elseif BanTime == "OneWeek" then
				BanData:SetAsync((Player), {BanStartTime = os.time(), BanTime = Times.OneWeek})
			elseif BanTime == "TwoWeeks" then
				BanData:SetAsync((Player), {BanStartTime = os.time(), BanTime = Times.TwoWeeks})
			end
		elseif BanType == "PermBan" then
			BanData:SetAsync((Player), {BanStartTime = os.time(), BanTime = Times.Infinite})
		elseif BanType == "Unban" then
			BanData:RemoveAsync((Player))
		else
			print("Ban Type Invalid!")
		end
	end)
	
	if not Success then
		print("Could not ban "..Player)
	end
end

HandlerRemote.OnServerEvent:Connect(function(Plr, BanType, UserName, Reason, Length)
	local PlayerToBan
	
	local success, errormsg = pcall(function()
		PlayerToBan = game.Players:GetUserIdFromNameAsync(UserName)
	end)
	
	if success then
		SetBan(PlayerToBan, Length, BanType)
		
		for i,v in pairs(game.Players:GetPlayers()) do
			if v.UserId == PlayerToBan then
				v:Kick("You have been banned! Rejoin to see your Ban Length.")
			end
		end
	else
		print("Error Fetching Player UserId")
	end
end)

(Ban Handler)

local BanData = game:GetService("DataStoreService"):GetDataStore("BanData")

game.Players.PlayerAdded:Connect(function(Plr)
	local Success, Result = pcall(function()
		return game:GetService("DataStoreService"):GetDataStore("BanData"):GetAsync(Plr.UserId)
	end)
	
	if Success then
		if Result then
			if not Result.BanStartTime + Result.BanTime > os.time() then
				Plr:Kick("Unban Date: "..os.date("%j", Result.BanTime))
			end
		end
	end
end)

(Client Handler)

local HandlerRemote = script.Parent.HandlerRemote

--Frames
local MainFrame = script.Parent.MainFrame
local List = MainFrame.List
local DropDown = List["2DropDown"].Drop

--Buttons
local CloseBtn = MainFrame.Close.TextButton
local OpenDropDown = DropDown.Parent.Button
local ConfirmBtn = List["5Buttons"]["1Confirm"].Button
local CancelBtn = List["5Buttons"]["2Cancel"].Button

--BanTypeBtns
local PermBanBtn = DropDown.PermBan.TextButton
local TempBanBtn = DropDown.TempBan.TextButton
local UnbanBtn = DropDown.Unban.TextButton

--Input
local UserName = List["1Name"].PlayerName
local Reason = List["3Reason"].BanReason
local Length = List["4Length"].BanLength

--Button Functions
function DropdownBtn(Name)
	script.Parent.SelectedBan.Value = Name
	DropDown.Parent.Button.Text = "Ban Type: "..Name
	DropDown.Visible = false
end

PermBanBtn.MouseButton1Click:Connect(function()
	DropdownBtn("PermBan")
end)

TempBanBtn.MouseButton1Click:Connect(function()
	DropdownBtn("TempBan")
end)

UnbanBtn.MouseButton1Click:Connect(function()
	DropdownBtn("Unban")
end)

CloseBtn.MouseButton1Click:Connect(function()
	MainFrame.Visible = false
end)

OpenDropDown.MouseButton1Click:Connect(function()
	DropDown.Visible = not DropDown.Visible
end)

ConfirmBtn.MouseButton1Click:Connect(function()
	if UserName and Reason and Length and script.Parent.SelectedBan.Value then
		HandlerRemote:FireServer(script.Parent.SelectedBan.Value, UserName.Text, Reason.Text, Length.Text)
		MainFrame.Visible = false
	end
end)

CancelBtn.MouseButton1Click:Connect(function()
	MainFrame.Visible = false
end)