Here’s a module that you can use. It returns time that is synced.
local monthStrMap = {
Jan=1,
Feb=2,
Mar=3,
Apr=4,
May=5,
Jun=6,
Jul=7,
Aug=8,
Sep=9,
Oct=10,
Nov=11,
Dec=12
}
local HttpService = game:GetService("HttpService")
local function RFC2616DateStringToUnixTimestamp(dateStr)
local day, monthStr, year, hour, min, sec = dateStr:match(".*, (.*) (.*) (.*) (.*):(.*):(.*) .*")
local month = monthStrMap[monthStr]
local date = {
day=day,
month=month,
year=year,
hour=hour,
min=min,
sec=sec
}
return os.time(date)
end
local isInited = false
local originTime = nil
local responseTime = nil
local responseDelay = nil
local function inited()
return isInited
end
local function init()
if not isInited then
local ok = pcall(function()
local requestTime = tick()
local response = HttpService:RequestAsync({Url="http://google.com"})
local dateStr = response.Headers.date
originTime = RFC2616DateStringToUnixTimestamp(dateStr)
responseTime = tick()
-- Estimate the response delay due to latency to be half the rtt time
responseDelay = (responseTime-requestTime)/2
end)
if not ok then
warn("Cannot get time from google.com. Make sure that http requests are enabled!")
originTime = os.time()
responseTime = tick()
responseDelay = 0
end
isInited = true
end
end
local function time()
if not isInited then
init()
end
return originTime + tick()-responseTime - responseDelay
end
return {
inited=inited,
init=init,
time=time
}
Paste it in a module (name it syncedtime
I guess) and require it:
local syncedtime = require(workspace.syncedtime)
syncedtime.init() -- Will make the request to google.com if it hasn't already.
print(syncedtime.time()) -- prints something like 1551081813.3696.
Since this isn’t an integer, you can use it in your loop and the result wouldn’t be choppy.
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")
local syncedtime = require(workspace.syncedtime)
syncedtime.init() -- Will make the request to google.com if it hasn't already.
-- I would just convert this to seconds since it's easier to work with.
local SECONDS_IN_A_DAY = 1*60
-- It's redundant to define a scale since we already have SECONDS_IN_A_DAY
-- local scale = 4
RunService.Heartbeat:Connect(function()
local t = syncedtime.time()
-- percentInCurrentDay goes from 0 to 1, 0 begin 00:00:00 and
-- 1 being 24:00:00
local percentInCurrentDay = t%SECONDS_IN_A_DAY / SECONDS_IN_A_DAY
-- Lighting.ClockTime can be decimal, and ranges from [0-24).
-- Use this rather than Lighting:SetMinutesAfterMidnight()
Lighting.ClockTime = percentInCurrentDay*24
end)