Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the point of the tick() function?

Asked by 5 years ago
Edited 5 years ago

I understand that tick() returns the amount of time since the UNIX Epoch (01/01/1970), what I don't get is what benefit that could possibly give? The UNIX Epoch was around 48 years ago, if it IS returning that number, than its a MASSIVE number (as it was a long time ago); Whats the point of having such a massive number, what kind of possible benefit could that number bring to your roblox game?

Also, what unit of time does the tick() function return the UNIX Epoch in?

0
tick is used for randomness, it also is used to time how long it takes to perform actions (helps you see which method is more efficient, etc) Vulkarin 581 — 5y
0
tick() returns from your local time (your computer lol) User#19524 175 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Not really sure if this helps but I use tick() with math.randomseed() making math.randomseed(tick()). This basically makes math.random more random.

Ad
Log in to vote
0
Answered by
ozzyDrive 670 Moderation Voter
5 years ago

Also, what unit of time does the tick() function return the UNIX Epoch in?

It is seconds since the epoch in UTC.

Developers can use the machine time to -- simply -- measure time as accurately as possible. The task scheduler uses the machine time to calculate the time difference between updates and uses that time to decide which actions to resume. Roblox's wait function returns the time it actually took to yield the thread -- that's passed in by the task scheduler. The RunService's events send the time delta between frames -- that's calculated with the machine time.

On other platforms you might not be provided with such events, in which case you need to calculate the delta yourself.

You'll eventually face situations where you'll be wondering: "how can I make this as accurate as possible?" Often there are multiple approaches, one of them is you using the machine time directly.

Say you want to calculate a packet's Round Trip Time (sender machine -> receiver machine -> sender machine):

local sendTick = tick()
RemoteFunction:InvokeServer()       --  remember to implement OnServerInvoke callback; it should only return nil and do nothing else
local elapsedTime = tick() - sendTick

In the previous case, there is no simple solution using wait/RunService.

Sometimes you want to know the time it took to complete an action that takes an unknown amount of time (maybe to simply collect data), for example, a server-wide matchmaker:

local mmStartTick = tick()
repeat
    --  plenty of logic, yields and requests
until opponentFound or playerCancelled
local mmEndTick = tick()
local mmElapsedTime = mmStartTick - mmEndTick

A simple timer could however be made with just the wait function:

local timeElapsed = 0
while true do
    timeElapsed = timeElapsed + wait()
    print(timeElapsed)
end

The tick would of course work there as well.

Answer this question