Time(), DeltaTime, or RunService.Stepped?

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

From the create.roblox.com site:

time(): number


Returns the amount of time in seconds that has elapsed since the current game instance started running.

os.time gives you an accurate measurement of time based on your computer’s time and shouldn’t be lagged by gameplay.

It looks like you can measure the differenct in os.time from the beginning of the server to the actual time using os.difftime

2 Likes

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?

The most efficient method is to use wait. The downside to this it that it runs slower (but is better for overall server load).

I recommend usage of os.clock for high-precision applications like springs and CFrame manipulations.

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.

1 Like

Hi!

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

Great, now I have no Idea of what I should be doing.

The point is that

  1. RunService.Stepped time is different across the computers.
  2. time() is different on server and on the client.
  3. 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.

It depends on what you are trying to do. If you want the time since a local game instance started, then you have to use time().