How to send and receive a server side timer for a hud

Essentially, the gamemode I’m trying to make a hud for has a timer which is server side. It works in the built in “timeleft” command, but i’m having a lot of trouble sending it to the client to use in a hud.

GetRoundsLeft() and GetTimeLeft() are printed from a function in init.lua, so I assume this will work in init.lua


net.Start("RoundsLeft")
	net.WriteString(GetRoundsLeft())
net.Broadcast()
net.Start("RoundTimeLeft")
	net.WriteString(GetTimeLeft())
net.Broadcast()

in cl_networkstrings.lua I have this, which presumably is fine


net.Receive("RoundsLeft", function(len)
RoundsLeft = net.ReadString()
end)
net.Receive("RoundTimeLeft", function(len)
RoundTimeLeft = net.ReadString()
end)

and in vgui/gamemode_timer.lua (a derma panel thingy) I have this


draw.DrawText("Round " .. RoundNumber .. " of " .. RoundLimit , "font", 149, 74, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
draw.DrawText(RoundTimeLeft, "font", 147, 0, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)

All of this (to me) looks like it should work. I took a string, sent it, recieved it, made it a global variable, and then tried to use it in another file. What’ve I messed up here? Any help it much appreciated

What’s your actual issue? Are you getting script errors?

From what you provided, RoundNumber and RoundLimit may not exist/may have not been networked.
These variables may also be nil between the moment the player starts seeing the HUD and receives the round data. You should make sure these variables exist before drawing anything. Like so:

[lua]
if (RoundTimeLeft) then
– draw stuff here…
end
[/lua]

Sorry, I always forget to say the actual issue.
The issue on the client side is that they are nil values, so presumably they haven’t been networked or cross file variable sharing isn’t a thing that exists

If you look at the cl_hud.lua in TTT, it uses this for round time left.


   local text = util.SimpleTime(math.max(0, GetGlobalFloat("ttt_round_end", 0) - CurTime()), "%02i:%02i")
   ShadowedText(text, "TimeLeft", time_x + margin, time_y, COLOR_WHITE)

For rounds left, TTT uses this


local function UntilMapChange()
   local rounds_left = max(0, GetGlobalInt("ttt_rounds_left", 6))
   local time_left = floor(max(0, ((GetGlobalInt("ttt_time_limit_minutes") or 60) * 60) - CurTime()))

   local h = floor(time_left / 3600)
   time_left = time_left - floor(h * 3600)
   local m = floor(time_left / 60)
   time_left = time_left - floor(m * 60)
   local s = floor(time_left)

   return rounds_left, string.format("%02i:%02i:%02i", h, m, s)
end

That doesn’t transfer between the serverside and the clientside does it? Unless you think I should just run a timer on both sides?

GetGlobalInt and GetGlobalFloat is networked already so it can be used on any side, unless it’s created clientside, then it can only be used clientside. So both of those can be used clientside.

Oh wow, that’s super helpful, thanks!