How do you make os.time() update faster?

Here I have a script which prints os.time() every .1 seconds:

while true do
    wait(.1)
    print(os.time())
end

The only issue is, os.time() updates once per second, and I need it to update every millisecond, is there any way I can do this?

use tick(). It has decimal precision.

while true do
    wait(.1)
    print(tick())
end 

But tick() is only the same in the timezone. It needs to be os.time(), as it’s the same in every timezone.

The only real use-case for using os.time() is for syncing time across servers or persisting time in a data store (like a timestamp). But yeah, it won’t give you the sub-second precision, so I would use tick() like @vtaetwrjxvdpgwjqkfzw suggested.

1 Like

What about
os.clock() + tick()%1 ?

2 Likes

That would work if you could guarantee that they’re synced up, but I don’t know if there’s any guarantee of that. Is there?

tick() returns a timezone dependent result, but (milli)seconds should be same across all timezones

2 Likes

For those wondering why I need it to be os.time() is because i’m making a ping detection system (Like in arsenal).

You can just use tick then. No need to use os.time

You don’t need os.time() at all then, just calculate the difference between before the ping and after the ping, no need to take care of timezones, and do all the calculations either on the client or on the server, but don’t try to sync both of them up when you’re simply trying to calculate the ping.

3 Likes
-- Client
local startPing = tick()
remoteFunc:InvokeServer()
local ping = (tick() - startPing)

You could do that over a period of time and average them out.

3 Likes

Would it work if I were to do this instead:

-- Server

spawn(function() -- Use spawn so that way if the player is hacking and never sends a response back to the server the server can at least still check the ping of other players.
    local startPing = tick()
    remoteFunc:InvokeClient(player)
    local ping = (tick() - startPing)
end)

?

1 Like

Personally, I would prefer using coroutine.wrap() instead of spawn(), but spawn is shorter to type.
I’m not sure of the accuracy of the method you’re using for measuring ping, so maybe you could do something like

note that the following method has some drawbacks (possibly laggier on the server, and it needs to fire the remotefunction two times from client for calibration)

and yes, I’m part of the semi-colon cult

Server

local off = {}; -- Offset table

remoteFunc.OnServerInvoke = function(plr, tm)
    local offset = off[plr.UserId];
    
    if not offset then
        if not off[plr.UserId-1]then
            off[plr.UserId-1] = os.time() - tm; -- First pass (timezone shifting compensation)
        else
            off[plr.UserId] = off[plr.UserId-1] - tm; -- Second pass (ping compensation)
        end;
    else
        local ping = (os.time() - tm) - offset; -- I'm not sure if it should be " - offset" or " + offset", haha
        print(math.floor(ping/1000).." ms");
        
        return ping;
    end;
end;

Client

local strt = tick();

-- Calibration

remoteFunc:InvokeServer(os.time()); -- Compensate for strange timezone offset (I've experienced issues with this before)
remoteFunc:InvokeServer(tick() - strt); -- Compensate for ping

-- Sending server the ping (and also getting it)

local ping = remoteFunc:InvokeServer(os.time()+tick()%1);
print(math.floor(ping/1000).." ms");

The reason why I compensate os.time() is because I’ve experienced issues with client’s os.time() not being lined up with server’s os.time(). (feel free to change that if you wish, but I’ve encountered issues relating to it)

Note that if two players are in the same game and have a UserId that’s the same just that with a offset of -1 you may experience issues with this method. This happens because I decided to do it this way for the sake of simplicity, but you can use two tables or edit the contents of the offset table in server to prevent it. (something like having the offset table be something like {Player = user_id, Time = tm, Passes = 0} and Passes ranging from 0 to 2 or something)

This code should work, it’s untested, so if you encounter any syntax error or such, it’s my fault, sorry.
Good luck! :wave:

2 Likes