• How to get the time of the server on the client?
    9 replies, posted
I want to get the current time of the server from the client but I forgot the function that does this, because I was sure there was a function for it. Any ideas?
Well it depends, are you talking about something like UNIX time or the time the server has been online (CurTime())? -snip look below-
Unix Time ;)
os.date or os.time?
[lua] if SERVER then local function SendTime(ply) umsg.Start("svr_time",ply) umsg.Long(os.time()) umsg.End() end hook.Add("PlayerInitialSpawn","SendTime",SendTime) end if CLIENT then os._SVRDiff = 0 local function GetSVRTime(um) local time = um:ReadLong() local dif = os.time() - time os._SVRDiff = dif end usermessage.Hook("svr_time",GetSVRTime) function os.ServerTime() return os.time() - os._SVRDiff end local function TCMD() print(os.time(),os.ServerTime(),os.date("%I:%M %p",os.time()),os.date("%I:%M %p",os.ServerTime())) end concommand.Add("print_servertime",TCMD) end [/lua] Tested and working correctly :smile: Use os.time() on the client to get the time based on the client's clock, use os.ServerTime() to get the time according to the server's clock.
[QUOTE=Gbps;20203494][lua]code[/lua] Tested and working correctly :smile: Use os.time() on the client to get the time based on the client's clock, use os.ServerTime() to get the time according to the server's clock.[/QUOTE] That's a pretty smart way of doing it, I would have had the server send the client the time every time the client requested it. Your way is obviously much more efficient.
[QUOTE=yakahughes;20203567]That's a pretty smart way of doing it, I would have had the server send the client the time every time the client requested it. Your way is obviously much more efficient.[/QUOTE] Well, I was basing my logic off the fact that time is consistent on every modern day computer, so you can simply give the client the offset of time and the computer's clock will do the rest.
[lua] if SERVER then local function SendTime(ply) umsg.Start("svr_time",ply) umsg.Long(os.time()) umsg.Long(CurTime())) umsg.End() end hook.Add("PlayerInitialSpawn","SendTime",SendTime) end if CLIENT then os._SVRDiff = 0 local function GetSVRTime(um) os._SVRDiff = os.time()-um:ReadLong()+um:ReadLong()-CurTime() end usermessage.Hook("svr_time",GetSVRTime) function os.ServerTime() return os.time() - os._SVRDiff end local function TCMD() print(os.time(),os.ServerTime(),os.date("%I:%M %p",os.time()),os.date("%I:%M %p",os.ServerTime())) end concommand.Add("print_servertime",TCMD) end [/lua] Accounts for network latency.
Thanks for your help.
[QUOTE=Deco Da Man;20205712][lua]if SERVER then local function SendTime(ply) umsg.Start("svr_time",ply) umsg.Long(os.time()) umsg.Long(CurTime())) umsg.End() end hook.Add("PlayerInitialSpawn","SendTime",SendTime) end if CLIENT then os._SVRDiff = 0 local function GetSVRTime(um) os._SVRDiff = os.time()-um:ReadLong()+um:ReadLong()-CurTime() end usermessage.Hook("svr_time",GetSVRTime) function os.ServerTime() return os.time() - os._SVRDiff end local function TCMD() print(os.time(),os.ServerTime(),os.date("%I:%M %p",os.time()),os.date("%I:%M %p",os.ServerTime())) end concommand.Add("print_servertime",TCMD) end [/lua] Accounts for network latency.[/QUOTE] Bah, that's what I should have done :saddowns: Thanks for that though.
Sorry, you need to Log In to post a reply to this thread.