Ban Script, banning people for longer than it should?

So I’ve recently stumbled across a lightweight admin system known as CMDR, and me and my Limited scripting knowledge has been trying to fix this for weeks,

So Starting off I have Ban Command and it uses return functions to work, so I’d like the code to remain relatively similar. It starts by formatting the time into a readable string for the ban message, which you can see via the Format and convertToHMS functions.
Then in a for loop it access’s a Database with the name being KGBan and the Scope being 5
It then creates a key (Player.UserId) and adds the necessary info inside it, Ban Start, Ban Duration, and the Ban Reason. It works perfectly so far, I’ve been banned for 1 minute ya da ya da, whatever

return function (_, players, length, reason, context)
	
	local function Format(Int)
		return string.format("%02i", Int)
	end

	local function convertToHMS(Seconds)
		local Minutes = (Seconds - Seconds%60)/60
		Seconds = Seconds - Minutes*60
		local Hours = (Minutes - Minutes%60)/60
		Minutes = Minutes - Hours*60
		local Days = (Hours - Hours%60)/60
		Hours = Hours - Days*60
		return Format(Days)..":"..Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
	end
	for _, player in pairs(players) do
		local DDS = game:GetService("DataStoreService");
		local Players = game:GetService("Players");
		local BanStore = DDS:GetDataStore("KGBan", 5)
		local function SetBan(Player, Reason, Duration)
			local Success, Error = pcall(function() 
				BanStore:SetAsync((Player.UserId), {BanStart = os.time(), BanDuration = os.time() + (Duration * 60), BanReason = Reason});
			end)
			print(Duration)
			Player:Kick("You have been banned for " ..convertToHMS(Duration).. " by an Admin for ".. Reason);
			if not Success then 
				warn("Not successful.")
			else
				print(Duration)
			end
		end
		local duration = length
		local playerToBan = player
			if playerToBan then
				if duration then
					if reason then
						SetBan(playerToBan, reason, duration);
						print("Player has been kicked!");
					end
				end
			end
		return ("Banned %d players."):format(#players)
	end

The problem comes as I have another script which should ban players on join. This is for if they still have an outstanding ban time, however when formatting the “Timer” it shows a number which is 10x that of the ban I gave my self.

Here is the block of code I have for it

game.Players.PlayerAdded:Connect(function(Player)
	print("Playerjoined")
	local DDS = game:GetService("DataStoreService");
	local BanStore = DDS:GetDataStore("KGBan", 5)
	local Success, Result = pcall(function()
		return BanStore:GetAsync(Player.UserId)
	end)

	local function Format(Int)
		return string.format("%02i", Int)
	end

	local function convertToHMS(Seconds)
		local Minutes = (Seconds - Seconds%60)/60
		Seconds = Seconds - Minutes*60
		local Hours = (Minutes - Minutes%60)/60
		Minutes = Minutes - Hours*60
		local Days = (Hours - Hours%60)/60
		Hours = Hours - Days*60
		return Format(Days)..":"..Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
	end

	if Success and Result then --see if datastore request was successful
		if (Result.BanDuration - os.time()) > 0 then --check if any data for the player was found
			print(Result.BanDuration)
			Player:Kick("You have been banned for " ..convertToHMS(Result.BanDuration).. " by an Admin for ".. Result.BanReason);
		end
	end
end)

So I guess what I’m asking is where might it be failing and how could I potentially fix this in the future. Any help would be appreciated.

1 Like

I gave my self a 1 minute ban, I even had it print out the duration in seconds. which it printed 60 seconds in a minute which can been seen in the output of the first image. so I’m unsure as to why I’m getting such a large ban, There is something else when checking the DataStore, I am greeted with the image posted below but am unable to understand what that means? Forgive my severe lack of knowledge.
image

Also note that the first image is when I ran the ban command, and the second is when I attempted to rejoin.

You’re displaying the Result.BanDuration which is os.time() + the ban time in seconds, try using:

Result.BanDuration - os.time()

os.time() returns the number of seconds from a certain date (around 1970), that’s where the large numbers come from for BanStart and BanDuration.

Would you mind clarifying where I put the snippet? I’m unsure where to put that as there are multiple snippets that could replace. Again Forgive me I’m pretty stupid when it comes to this.

right here in the second block of code you posted, in the Player:Kick() argument