Properly using timer.TimeLeft

I need some help with a timer. I’m creating a timer on the server side, and I want to use timer.TimeLeft(“TimerName”) on the client side to retrieve that remaining time. I tried using the net library, but can’t figure it out, could someone point me in the right direction?•

Also I’m very new to coding so a lot of things you will need to spoon feed, besides that, thank you to everyone who attempts to help me.

It’d probably be better to make the timer shared, or send a net message and make a timer on client at the same time and use it as a pseudo type of deal

That’s what I did and failed, could you whip me up so code showing how to do it? It would be much appreciated.

Can you post what you’ve done already?

Unfortunately I deleted the code due to the lack of success. Let me try to attempt at it one more time and I’ll give you what I got.

[editline]26th April 2016[/editline]




server:

function newRound()
	round_status = round_status + 1
	updateClientRoundStatus()
	RoundTimer()
end

function RoundTimer()
	timer.Create("Round Timer", 180, 0, function()
	if timer.TimeLeft("Round Timer") < 1 then
	newRound()
		end
	end)
	print (timer.TimeLeft("Round Timer"))
end

net.Receive("RoundLength", function()
	timer.TimeLeft("Round Timer")
end)

client:
hook.Add("HUDPaint", "RoundValue", function()
	net.Start("RoundLength")
	net.SendToServer()
draw.SimpleText(timer.TimeLeft( "Round Timer" ), "Simple", x / 2 - 5, 25, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	
end



I have a button which runs the function newRound()
also it does print 180 in my console when it is pressed.
I’m pretty sure this is what I had before.

Alternatively, you could store CurTime() in a variable, send it to the client and then rewrite your code around that. Then you wouldn’t have to mess around with networking timers.

Since you’re new to coding, CurTime() is literally just the amount of seconds since January 1st, 1970, 00:00 AM. If you save it in a variable and then CurTime() - the variable you get the difference between those times

Okay, you’ve got your net.Start inside a HUDPaint hook - that’s ran every frame meaning you’re constantly sending a net message and that’s bad

Another thing is, if your original code is serverside (which your timer is), then you want your net.Start to be serverside and net.Recieve clientside (and if you wanted to send data from client to server, you’d swap those two around)

So here’s what you do - put the net.Start inside RoundTimer(), so the server knows to send a message to the client on round start. Put your net.Recieve anywhere clientside, then inside it create a timer for the appropriate length - lastly in your HUDPaint hook use timer.Exists and then draw the appropriate variable with timer.TimeLeft

Found a better solution.