How can I make a timed ban?

  1. What do you want to achieve?
    I want to achieve a punishment system.

  2. What is the issue?
    I just can’t figure out how to make a timed-ban sorta thing.

  3. What solutions have you tried so far?
    Nothing has helped so far.

I don’t have code for it because I just can’t figure out how to get make it.

EDIT: I do have server-bans working.

Bans are basically just kicks when a user joins a server. You need to check the data of banned users. Alongside holding a table of banned users, you can attach a time when the ban will expire. When a user joins and you check bans, do an additional check to see if the time now is >= expireTime, if it is remove the ban and let the user join as normal. expireTime is just an example you can save it as whatever, you just need a way to get expire time from user id preferably.

I know how to do that. I just can’t figure out how to make it timed.

I’m not sure exactly how your ban system works, but I would just have a table in a datastore that holds all banned user’s and expire times. It would look something like this:

{
    [1234] = expireTime
}

So if user with id 1234 joins you could retrieve ban data just by doing bans[userId] and ensuring it isn’t nil. I would also use tick() + time in seconds to ban. So you know when they join if tick() >= bans[userId] their ban expired. These are just example variables of course, I don’t know a lot about your ban system.

Here is my format for the bans:


    {

    UserId = 0,

    Username = "",

    Moderator = "",

    Length = {days, hours, minutes},

    InitTime

    Reason = ""

}

EDIT: I don’t understand tick yet.

tick() is the time in seconds since January 1st, 1970 (the epoch date). Basically, if you save the time a ban expires, in seconds, you can just compare it to the current tick(). This is why you should get the time, format it into seconds (i.e. 1 day is 86400) and offset it by the current tick(). So tick() + 86400 will result in the time in seconds exactly one day from now. So when you check if a player is banned, you can compare the current tick() to the time their ban expires using tick() >= theTimeTheirBanExpires. If this returns true, you know exactly one day, or longer has passed. If it’s false, then the ban has not yet expired.

1 Like

I suggest you using os.tick() to store the starting ban data, and then taking into account 1 day = 86400 seconds, do 86400 * Days Banned, and that will return the expiration date. To get the format do, Starting Ban + Expiration date, and the use os.date() to format it.

1 Like

Could this work?

 Expiration = math.floor(tick() + (length[1] * 86400) + (length[2] * 3600) + (length[3] * 60)),

Then it would check if the current date is equal to that.

Looks good, given that your length array is days, hours, minutes. So tick() >= Expiration should do the trick.

I am going to test my code rn to see if it works.

I made a ban system time ago,

https://gyazo.com/59c0029ffb3207ff2bee70b1e0864d91
https://gyazo.com/91e122e43a36c095a55a6e3456fc1ce9

And formatting with:

local TimeLeft = (tonumber(TempData["Issued Date"]) + tonumber(TempData["Total Period"]))
        local TimeLeftFormat = os.date("%m/%d/%y | %H:%M:%S", TimeLeft)
        local BannedDate = os.date("%m/%d/%y | %H:%M:%S", TempData["Issued Date"])

I keeps saying I am banned for a really big number when the number should be 60.

Try using os.time() instead of os.tick()

That’s not the point of this. Using an expiry date is the time it expires at, not the duration of the ban. If you want the remaining duration of the ban it’d be ExpiresAt - tick(). Which is again not the point of an expiry date.

Still, I have the length set as {0,0,2} which is 2 minutes. It then says I am banned for like millions of seconds

Please refer to what I said about tick() it’s the time since January 1st, 1970. This is an expiry date, not a duration.

Okay, now rn at 6:12 PM I tested it and it says I will be unbanned at 10:13 pm.