Just a Simple Question, which one of them should I use when when it comes to telling how much time has passed since the Game Began? and with one would be more accurate?
-- time()
time()
-- DeltaTime
local Current = 0
Current += task.wait() -- because task.wait() will return deltaTime
-- and is about the same as RunService.Heartbeat
-- Stepped
local time, deltaTime = RunService.Stepped:Wait()
-- Stepped returns both Time and DeltaTime
Yes, but thats not really what I’m asking, I mean the Time since the Game Started, as in the Game Instance, which is what time(), and RunService.Stepped return.
Just asking which one is more accurate or Efficient to use, or should I do something else?
I just track this manually using DateTime (I don’t use os.clock because I don’t need high precision for a server instance uptime calculator). Whenever my main script starts running, I just check what time it is and then record that. If I want the client to be able to track this as well, I just send the time over and apply client-side prediction to continue counting the server’s uptime on a UI.
There’s no sense in using any yielding functions here for determining a peer’s uptime. Use a time function to record a starting baseline and use prediction to continue counting for any display purposes or simple math for non-display purposes.
Colbert, I hope you don’t mind me extending your answer a little bit. I believe yours is the solution.
time() | Time since the game instance began. Definitely the most accurate one if you are looking for the absolute beginning. Unique on server and any client.
time from RunService.Stepped | Elapsed time since RunService started running. Unique on the server and any client.
task.wait() delta | Delta time from previous frame (Heartbeat), in a loop it can be heavily affected by lag, thus it’s not a good option on its own.
Even if frame rate is significantly lower than normal, time() and RunService.Stepped time are going to be almost identical (RunService starts microseconds later).
That’s it. No delta time from yielding functions/events is necessary.
elapsed = initialTime - currentTime -- (same time zone, same units)
I like to use workspace:GetServerTimeNow() (since server epoch).
task.wait() at 60 Hz yields for 0.01667 seconds at best, so it introduces a continuously increasing offset, which eliminates counting from the most accurate options. For a second-precision counter, it’s very easy to compensate for the error.
local initialTime = workspace:GetServerTimeNow()
while true do
local offset = (workspace:GetServerTimeNow() - initialTime) %1
task.wait(1 - offset)
print(workspace:GetServerTimeNow() - initialTime)
end
RunService.Stepped time is different across the computers.
time() is different on server and on the client.
yielding functions with a counter will offset, sooner or later.
The best you can do is get a time stamp. Doesn’t matter what you use, DateTime (with appropriate tokens), workspace:GetServerTimeNow(), whatever can also be read by the client. Whatever you can send to the clients and have them continue on their own.
server age = initial time - current time
task.wait() counter vs. time stamp
local initialTime = workspace:GetServerTimeNow() - time()
local counter = 0
task.spawn(function()
while true do
counter += task.wait()
end
end)
while true do
local offset = (workspace:GetServerTimeNow() - initialTime) %1
task.wait(1 - offset)
print("Server time: "..workspace:GetServerTimeNow() - initialTime)
print("Counter: "..counter)
end
→ Depends on frame rate.
RunService.Stepped time vs. time stamp
local RunService = game:GetService("RunService")
local initialTime = workspace:GetServerTimeNow() - time()
while true do
local offset = (workspace:GetServerTimeNow() - initialTime) %1
task.wait(1 - offset)
print("Server time: "..workspace:GetServerTimeNow() - initialTime)
print("RunService: "..RunService.Stepped:Wait())
end
→ Not affected by lag;
→ different on client;
→ runs with a negligible delay.