Figuring out exactly when a player will be unbanned

If you don’t understand the title, here’s a more detailed explanation.

Basically, I created my own ban system with ProfileService and it works well. I made ban lengths and things like that.

The problem is, I want the player to know WHEN they will be unbanned. Not just them staring at a “You’re banned” screen, thinking they have been permabanned.

The way I’m trying to do it is by taking the current time when they were banned and adding the ban length (in seconds) onto it.
local formattedTime = DateTime.now():FormatLocalTime("LLLL","en-us")

For example:
Let’s say a player gets banned on December 19th, at 4:30 PM. Their ban will be… 3 days long. This equals to 259200 seconds. I want to add these seconds onto December 19th, 4:30 PM and somehow end up with December 22nd, 4:30 PM.

Is this even possible?

1 Like

Of course this is possible, one of the most easy methods would be to make use of the Unix timestamps which represent the number of seconds since January 1, 1970 from the DateTime object.

Here’s an example to do so:

local function getUnbanTime(banDurationInSeconds)
	-- Get the current time as a Unix timestamp
	local now = DateTime.now().UnixTimestamp

	-- Add the ban duration to the current time
	local unbanTimestamp = now + banDurationInSeconds

	-- Convert the Unix timestamp back to a DateTime
	local unbanTime = DateTime.fromUnixTimestampMillis(unbanTimestamp * 1000)

	-- Format the unban time to a readable format
	local formattedUnbanTime = unbanTime:FormatLocalTime("LLLL", "en-us")

	return formattedUnbanTime
end

local banLengthSeconds = 259200
local formattedUnbanTime = getUnbanTime(banLengthSeconds)

print("You will be unbanned on: " .. formattedUnbanTime)

Works perfectly, thank you. I knew it had something to do with those timestamps but it would’ve taken so long to figure out.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.