-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathservertime.lua
43 lines (34 loc) · 1 KB
/
servertime.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/servertime.lua
-- synced shared unix time getter with ms precision (serverside timezone)
-- this required for stabillity between restarts (eg saving entity cooldown dt between restarts)
ServerTimeOffset = ServerTimeOffset or 0
local offset = ServerTimeOffset
function ServerTime()
return offset + CurTime()
end
if SERVER then
ServerTimeOffset = os.time() - CurTime()
offset = ServerTimeOffset
local function Sync(targets)
net.Start("ServerTimeSync")
net.WriteDouble(offset)
net.Send(targets)
end
util.AddNetworkString("ServerTimeSync")
Sync(player.GetHumans())
local queue = {}
hook.Add("PlayerInitialSpawn", "ServerTimeSync", function(ply)
queue[ply] = true
end)
hook.Add("SetupMove", "ServerTimeSync", function(ply, _, cmd)
if queue[ply] and not cmd:IsForced() then
queue[ply] = nil
Sync(ply)
end
end)
else
net.Receive("ServerTimeSync", function()
ServerTimeOffset = net.ReadDouble()
offset = ServerTimeOffset
end)
end