Get time from arbitrary amount of seconds at any point in time

I am trying to script a ban system which displays the days, hours minutes and seconds that are left to unban. I store the duration as os.time() + SECONDS_TO_BAN and then check if the duration is bigger than the current os.time() when a player connects to check if they are still banned. I also want to display the days, hours minutes and seconds left, how can I do so?

1 Like

Client-side prediction. Send banExpiryTimestamp - currentOsTimestamp to the client when their ban information needs to be displayed and have it count down that given time with Heartbeat. Subtract deltaTime given to Heartbeat from the sent time and round it down.

Think of it in terms of:

-- Receive time from the server
Foobar.OnClientEvent:Connect(function (banTimeLeft)
    receivedBanTime = banTimeLeft

-- Connect this after receiving the time
Heartbeat:Connect(function (deltaTime)
    receivedBanTime -= deltaTime

I assume you just need a way to format a time (in seconds), into hours, minutes & seconds.

local function formatTime(totalTime)
	local hours = math.floor(totalTime / 3600)
	local minutes = math.floor((totalTime - (hours * 3600))/60)
	local seconds = (totalTime - (hours * 3600) - (minutes * 60))
	local format = "%02d:%02d:%02d"
	return format:format(hours, minutes, seconds)

local formattedTime = formatTime(50000) --Example seconds till ban expires.
print(formattedTime) --13:53:20

Hi, thanks for the answer however the problem is I dont have the banExpiryTimestamp but only the duration a ban would have and the os.time() timestamp at the time of the ban which is checked with another current os.time() timestamp when the user rejoins the game. For example something like this

duration = os.time() + SECONDS_TO_BAN

ban(player, duration)

    if player is banned then
        if os.time() > duration then

Hey, thanks for answwering but that will only convert the constant to a static formatted time, I want to do it at any point in time, such as when the player joins the game again while they are banned and display time remaining…

It should be fairly simple to understand that you can call the function and pass to it any number value, this might be beyond your scope of understanding.

Change the “50000” in the code snippet I provided.

I understand that, however my question does not only ask on how to convert a timestamp into days, hours minutes and seconds but how to actually get it from the circumstances I have provided verbatim

You store the duration as os.time() + SECONDS_TO_BAN, all you need to do is pass SECONDS_TO_BAN to the function.

I apoligize if I am being unclear, that will only show a static result that is only applicable when they are banned initially, I also want to show the days, hours… when they join back at a later time corresponding to the time left currently. For example, lets say the seconds to ban is 604800 (1 week)… it will keep showing 7 days, 0 hours, 0 minutes and 0 seconds even when they join 2 days before their unban time or whenever…

Your banExpiryTimestamp is the duration. When you save the current value of os.time() plus the number of seconds that the ban lasts for, this creates a future Unix timestamp in seconds. Those number of seconds when retrieved from a DataStore is your banExpiryTimestamp.

When the value is retrieved from the DataStore, you subtract the current value of os.time() from whatever the DataStore returns and that’s the number of seconds left in the ban.

local banTimestamp = os.time() + 60


local timeLeftInBan = banTimestamp - os.time()

print(banTimestamp, timeLeftInBan)

The timeLeftInBan variable is the number of seconds, so you can do anything with that. If you’re just using Kick then format it on the server first and then append it to the kick message (it will not dynamically change, kick messages are static). If you want a Gui displaying that time then send timeLeftInBan to the client via a RemoteEvent and count it down. Sample in my first post.