Display Round Time Left On HUD

Could someone please show me a simple example on how to display how much time is left on the HUD using this code as the round system.
[LUA]
if CLIENT then

local function TimerStatus()
timetowrite = “Round Finished”
end

usermessage.Hook(“TimerStatus”, TimerStatus)

function ReceiveTime( um )
roundtime = um:ReadShort()
timetowrite = "Time: " … string.ToMinutesSeconds(tostring(roundtime))
end

usermessage.Hook(“SendTime”, ReceiveTime)

function DrawTime()
draw.SimpleText(timetowrite, “HUDNumber5”, ScrW() / 2, 20, Color(255, 0, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end

hook.Add(“HUDPaint”, “DrawTime”, DrawTime)

end

if SERVER then

roundtime = 600

local function SetupNewRound()
timer.Simple(10, function()
roundtime = 600
timer.Start(“RoundTimer”)
end)
end

function RoundTime()
roundtime = roundtime - 1
umsg.Start(“SendTime”)
umsg.Short(roundtime)
umsg.End()
if roundtime == 0 then
umsg.Start(“TimerStatus”)
umsg.End()
for _, ply in pairs(player.GetAll()) do
ply:StripWeapons()
ply:StripAmmo()
end
SetupNewRound()
end

end

timer.Create(“RoundTimer”, 1, roundtime, RoundTime)

end
[/LUA]

[editline] oh dear [/editline]
Ah, I see now, you needed a method of display, not a time-keeping system. Whoops.

That didn’t really help me because I don’t how to display the timer on the HUD. Here is what I got but I get this error when I run it. [@lua\includes\extensions\string.lua:102] bad argument #1 to ‘floor’ (number expected, got string)

[LUA]
function MainHUD()
–Round Timer
draw.RoundedBox(0,ScrW() / 2, ScrH() - 53, 90, 50 , Color(0, 0, 0, 180))
draw.SimpleText(“Timer”, “Trebuchet19”, ScrW() / 2 + 25, ScrH() - 4, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM)
draw.SimpleText(string.ToMinutesSeconds(tostring(roundtime)), “HUDNumber”, ScrW() / 2 + 45, ScrH() - 15, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM)
end
hook.Add(“HUDPaint”, “MainHUD”, MainHUD)
usermessage.Hook(“SendTime”, MainHUD)
[/LUA]

Change
[lua]
draw.SimpleText(string.ToMinutesSeconds(tostring(roundtime))…
[/lua]

to this:
[lua]
draw.SimpleText(string.ToMinutesSeconds(roundtime)…
[/lua]

Because floor() isn’t anywhere in what you just posted the issue’s gotta be in that exterior function.

Okay thank you that fixed that problem. I still get a problem though the time just displays 00:00 and doesn’t do anything.

Well your time system isn’t doing what it’s supposed to, keeping time. You never tell the server to send the time to the client. Timers don’t do what you think they do. I guess I’ll re-write my example code again.

Ah okay, and okay thank you :slight_smile:

Alright, here you are. Essentially a re-write of your original system but it works now.

[lua]
– A simple time-based round system

if CLIENT then
local timeleft = 0

-- Sets the time left and returns nice output for GUI
local function TimerStatus()
	usermessage.Hook("TimeLeft", function(um) timeleft = um:ReadShort() end)
	
	if timeleft > 0 then return timeleft else return "Round Finished." end
end

-- Draws your shit
local function DrawTime()
	draw.SimpleText(TimerStatus(), "HUDNumber5", ScrW() / 2, 20, Color(255, 0, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
hook.Add("HUDPaint", "drawtime", DrawTime)

else
– Round will be a minute long (seconds)
local roundtime = 60
local roundstart = 0
local timeleft = 0

-- Sets up a round. The tim when the round was started is used as the base. 
-- Then, the timeleft is as simple as subtracting the old time from the new.
local function SetupNewRound()
	timeleft = 0
	roundstart = CurTime()
end

-- Sends the time and also checks for new round. Hooked into "Tick" to update
-- the client every tick of the time left.
local function SendTimeLeft()
	timeleft = roundtime - (CurTime() - roundstart)
	umsg.Start("TimeLeft")
		umsg.Short(timeleft)
	umsg.End()
	
    -- Do your end-of-round stuff here.
	if timeleft <= 0 then
		for _, v in pairs(player.GetAll()) do
			v:StripWeapons()
			v:StripAmmo()
		end
		
		SetupNewRound()
	end
end
hook.Add("Tick", "sendtimeleft", SendTimeLeft)

end
[/lua]

[editline]7th February 2012[/editline]

Fishbrain, be aware I made a small mistake on line 34. Instead of 60, that should be “roundtime”.

Thank you, but what I’m I ment to put in these so I can display the timer on the HUD:
[LUA]
draw.SimpleText(string.ToMinutesSeconds()
[/LUA]
And this.
[LUA]
usermessage.Hook("", MainHUD)
[/LUA]

And okay yeah I changed that bit of code but still cant figure out how to display on HUD. sorry quite tired lol

Implementation of ToMinutesSeconds for the rewrite:
[lua]
function ToMinutesSeconds(a)
if type(a) == “number” then
local minutes = a / 60
local seconds = a % 60
return string.format("%0.2d:%0.2d", minutes, seconds)
else return a end
end
[/lua]

Put that where you like so you can access it when you need to. To use it just do ToMinutesSeconds(TimerStatus())

[editline]7th February 2012[/editline]

As for that MainHUD usermessage hook, what do you mean?

[editline]7th February 2012[/editline]

I’ve also left some programming fun for you. Problem: when the script starts it autoruns the StripWeapons() and StripAmmon() part of the time check despite the timer never running. How do you fix this? (Hint: something to do with booleans).

Cheers. And okay I’m sure that will be fine.