So I’m having a hard time understanding what’s the most efficient way of getting the global time. My current idea is just using the FireAllClient, but I have a feeling that this might cause some server lag since it’s connected to a RunService.Heartbeat:
function GlobalTime:Start()
local CurrentTime = os.clock()
local function FireGlobalTime()
if CurrentTime ~= os.clock then
CurrentTime = os.clock()
self:FireAllClients(GLOBAL_TIME_EVENT, CurrentTime)
end
end
RunService.Heartbeat:Connect(FireGlobalTime)
end
My second idea is getting the global time on the client side with the use of remote function; still connected to a RunService.Hearbeat:
-- Client Side
function GlobalTime:Start()
local function GetGlobalTime()
print(GlobalTimeService:Get())
end
RunService.Heartbeat:Connect(GetGlobalTime)
end
-- Server Side
function GlobalTime.Client:Get()
return os.clock()
end
Or are they just the same? Or is there an alternative way of getting the global time not the player’s current time, since os is based on the player’s operating system.
You can fire a RemoteFunction from the client to server asking for the time and the server will return the time, you can have a 1 second delay between each fire, this wont affect the game lag since it’s done on the client.
I forgot that DateTime exist I could just use this to get the UTC/GMT or Player’s local time which is what I want:
function GlobalTime:Start()
local function RealDayNightCycle()
local Now = DateTime.now()
local LocalTime = Now:ToLocalTime()
Lighting:SetMinutesAfterMidnight(LocalTime.Hour * 60 + LocalTime.Minute + (LocalTime.Second / 60))
end
RunService.Heartbeat:Connect(RealDayNightCycle)
end