Lua keeping track of time?

Is it possible to make a lua to keep track of time? Such as you say !addtime day and it waits a day then it does something? I am asking this because I have an idea for making an automatic VIP tracker. Like “!addvip infernalcookie month” then it will add 31 days or 10800 minutes and then it will remove him from the usergroup or Team().

I’m also pretty sure it’s possible, because admin mods keep track of ban times, right? Or is the built in ban do it for you?

**[G.SysTime

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.SysTime)**

Im not sure if this is the best way of doing it but when you give the person vip status you can add that player to a table with the system time + time you want to give them and make it check if its past that time when they join the server, so it should work as long as the system time dosent change

bekka’s is better use os.time

os.time[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb]

Shameless plug time!

http://www.facepunch.com/threads/981910

Put this in a server-side Lua file:
[lua]require ‘cron’
cron.RegisterJob( ‘unvip’, function( sid )
– do stuff here, sid is the player’s SteamID
end )[/lua]

Then, put this inside the command you run to add VIP players (it’s one month here):

[lua]-- Calculate unvip time
local t = os.date( “*t” )
local conf = {
Minutes = t.min,
Hours = t.hour,
Days = t.day,
Months = (t.month%12 + 1)
}
– Create schedule
cron.Add( “unvip_” … ply:SteamID(), conf, “unvip”, ply:SteamID() )[/lua]

But would I have to check if he said month then add 10800 minutes to time? Then if the time == that result then revoke vip? And how would I add minutes…

I have no idea how this works nor how to use it. Tried going back to your thread and reading on cron… Doesn’t make any sense.

It’s pretty much the same as GMod’s schedule library, except they aren’t temporary. The code I posted should work for the taking time part, the VIP access storing and removing you’d have to do yourself. As for the cron syntax, you won’t really need it here, as I’ve abstracted it enough.

For better explanation of the code:
[lua]require ‘cron’ – first, load the cron module

– here we register the job which will be ran later
cron.RegisterJob( ‘unvip’, function( sid )
– do stuff here, sid is the player’s SteamID
end )[/lua]

[lua]
– let’s configure the date and time we want the job to be ran
local t = os.date( “*t” ) – table of current date and time
local conf = {
Minutes = t.min, – run at the same minute,
Hours = t.hour, – same hour,
Days = t.day, – and day
Months = (t.month%12 + 1) – but add one to month
}

– creating the schedule
– first argument is the schedule id, I make it unique per player here
– second is the config table where we specify when we want it to run
– third argment is the id of the job we wat to run, registered in above example
– lastly, optional arguments, which are stored in the data file and passed
– as arguments to the job when time comes
cron.Add( “unvip_” … ply:SteamID(), conf, “unvip”, ply:SteamID() )[/lua]

I hope it helps :smile:

So in
[lua]
cron.RegisterJob( ‘unvip’, function( sid )
[/lua]

I would do the function for revoking their vip and it will be called when the time is up?

But I still don’t understand how to add an amount of time, say a month or 6 months or a year, etc.

You call cron.Add and pass a unique index, a table containing the time information, the index of the linked cron job, and the arguments.

cron.Add("<UNIQUE_INDEX>" , {TABLE OF DATA ABOUT WHEN THE JOB SHOULD BE TRIGGERED} , “<UNIQUE LINKED JOB>” , “<ARGUMENTS>”)

[editline]12th February 2011[/editline]

[lua]-- let’s configure the date and time we want the job to be ran
local t = os.date( “*t” ) – table of current date and time
local conf = {
Minutes = t.min, – run at the same minute,
Hours = t.hour, – same hour,
Days = t.day, – and day
Months = (t.month%12 + 1) – but add one to month
}[/lua]

the “+1” adds one month to the time table.

Okay so I think I got it down, but how do I call it?