Temp Ban system not working

  1. What do you want to achieve?
    I want to ban the player for a selected amount of time.

  2. What is the issue?
    Everytime I join after I got banned the time doubles.

  3. What solutions have you tried so far?
    I looked around on the DevForum.

BanScript

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("SimpliCmdBans4")
local SS = game:GetService("ServerStorage")

local module = {
	
	Ban = function(plr, reason, banLengthM, banLengthH, banLengthD)
		
			local Id = plr.UserId
			
			local banData = {
				
				banned = false,
				startTime = 0,
				endTime = 0,
				reason = ""

			}
			
			banData.banned = true
			banData.startTime = os.time()
			banData.endTime = banData.startTime + (banLengthM * 60 + banLengthH * 3600 + banLengthD *  86400)
			banData.reason = reason
			
			DS:SetAsync(Id, banData)
			plr:Kick("Banned".."\n"..reason.."\n".."Time left: "..tostring(banData.endTime - os.time()).." seconds.")
	end,
	
	UnBan = function(plr)
		local Id = plr.UserId
		
		local banData = {

			banned = false,
			startTime = 0,
			endTime = 0,
			reason = ""

		}
		
		DS:SetAsync(Id, banData)
	end,
	
}

return module

Check ban script

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("SimpliCmdBans4")
local SS = game:GetService("ServerStorage")
local cmds = require(script.Parent.Commands)

local module = {
	
	turnToTime = function(timeNumber)
		
		local minutes = timeNumber / 60
		local hours = minutes / 60
		local days = hours / 24
		
		return minutes, hours, days
		
	end,

	CheckBan = function(plr)

		local Id = plr.UserId

		local banData
		local success, warnd = pcall(function()
			banData = DS:GetAsync(Id)
		end)
		
		if success then
			if banData.banned then
				
				if banData.endTime - banData.startTime <= 0 then
					cmds.UnBan(plr)
					print("Player unbanned")
					return
				end
				
				local minutes = (banData.endTime - banData.startTime) / 60
				local hours = minutes / 60
				local days = hours / 24
				
				
				
				if minutes < 0 then minutes = 0 end
				if hours   < 0 then hours   = 0 end
				if days    < 0 then days    = 0 end
				
				
				
				cmds.Ban(plr, banData.reason, minutes, hours, days)
			end
		end
		

	end,

}

return module

(The “CheckBan” function gets called when a player joins the game)

If when you join the time doubles, then I think it’s the cmds.Ban in your Checkban that’s causing the issue

When you ban yourself, it works normally, makes you banned, puts the start and end times and sets a reason, puts it in the datastore before kicking you

When you join back, which I’m assuming you join back before the ban time ends, the game notices you’re banned, to which it calls the Ban function again, the problem with this is that the start and end times are going to change since start time will now be when you rejoined rather than when you got originally banned, which messes with the end time as well

I think rather than run the ban function again in Checkban, it’s best to kick right away if their time hasn’t ended yet, of course doing your reason formatting as well

2 Likes

Damn I didn’t even think about that, its so obvious now, thanks.

1 Like