How to make a hud timer?

I’m trying to make a timer for my server that is displayed on everyone hud. It’s going to countdown from 30 min, then say that the purge has begun, then countdown from 10 min. My problems: I’m not good with lua so how would I make one? Also, I don’t know here I would put the timer when I’m done (I use darkrpmodification addon).

All help appreciated!

(I looked on the wiki, but didn’t understand.)


So, uhm, I’ve never actually seen or played The Purge, but from what you’re asking for, I’m going to assume it’s something like this;
function StartPurge(startmins, endmins)
– do your shit here when the purge starts
timer.Create(“PurgeCountdown1”, startmins * 60, 1, function()
– do your shit here when the purge countdown 1 ends, and the purge countdown 2 starts!
timer.Create(“PurgeCountdown2”, endmins * 60, 1, function()
– do you’re shit here when the purge countdown 2 starts?..
– do your shit here when the purge ends??
Then, to start this you would just run StartPurge(30,10) – Which is what you described?.. So, if you then want to draw this on a HUD, you need to use timer.TimeLeft which returns a number as stated above.
timer.TimeLeft(“PurgeCountdown1”) – for countdown 1…
timer.TimeLeft(“PurgeCountdown2”) – for countdown 2…
The rest is pretty obvious. (Don’t blame me if I understood The Purge wrong in any way)

Thank you so much, but… I don’t know where to put these codes for my server. (i.e. what file to paste it into.)

That’s not a complete addon, so theres no drag & drop content. It’s pretty obvious, though. You could just put it in shared to keep it all simple 'n shit, and use if SERVER then, if CLIENT then… etc, but that’s up to you.

That’s the problem, I have no idea what shared.lua to put it in. If anyone could put a filepath that would be great.

Here’s a tutorial that shows how to do a count-down ( with networking; not each second, but simple networking of initial data so it is light-weight ).

It could be made shorter by using a timer, and using timer.TimeLeft ( The method described in the tutorial is the “original” way of calculating time-remaining vs elapsed, and is still used in a lot of places )

Hopefully these tutorials and information helps. As always, to view the Lua from any of my tutorials to enable copy/pasting ( HTML doesn’t copy/paste well in terms of HTML / CSS Highlighted Lua ), remove .html from the url.

I don’t know what file to make my timer in, so just… what file? (i.e. does it need to be in cl_init.lua or addons folder ect.)

Both. Server and client.

If they’re both in sync, you’ll have a “SHARED” result where both call something at the end.

In my example it shows both. So a start time is set, CurTime is synced between clients and server; using CurTime( ). Then, the duration of the timer is set, so that CurTime( ) - StartTime produces elapsed time, whereby ( Duration - ( CurTime( ) - StartTime ) ) produces time-remaining.

That way ( if you do both ), you can have the client do something with HUDPaint to draw the remaining / elapsed time. And the server will simply manage the data and execute the timer when it is ready ( I’m pretty sure I used timers in that example so it is fired at the end… I may have given them a name too, which is useful if you want to timer.Adjust the time to extend to reduce time )

You seem to not be able to get that I don’t know where to put any of this shit. My brain needs a file directory. Example: C:\GMOD\garrysmod\addons

The simple method I used for my Door Locker was to create the timer of the Server and then create a networked float which was (timer + curtime) and then Clientside I subtracted (curtime - networked float) and used that.

facepalm for days. :pwn:

Oh you dont know where to put it! That was the hardest thing for me to learn. Perhaps try Lua/autorun! Thats my goto for just about everything

Finally thank you! One thing, where in lua/autorun?

In lua/autorun. No further. Put it there. :v: Well, that’s for shared. the two folders are pretty obvious. Server in server, and Client in client.

so how do I put the code in lua/autorun? do I make a new .lua with the lines in it?

Yes :v:

You know what, before doing this, take some time to read a few lua tutorials, anything you can find that explains clientside/serverside differences

It’s essential you know them and it’ll help you later down the line

I get this error in my server console:

[ERROR] lua/autorun/timer.lua:6: unexpected symbol near ‘)’

  1. unknown - lua/autorun/timer.lua:0

this is line 6:

timer.Create(“PurgeCountdown2”, endmins * 60, 1, function()

Go figure out how lua works, and then try to code some stuff. It was already stated that what he posted wasn’t a full addon, and I’m assuming you just pasted that in there as is