Tick() How do I use it?

I checked the developer wiki, tried looking up youtube videos. And yet I still have no idea how to use it, please help.

34 Likes

Tick() provides the time in seconds that has passed since 1st of January of 1970. It actually provides quite a few uses.

What it is most used for is counting how many seconds have passed from one point in the script to another, since tick() is always increasing second by second.

An example of its use would be the following:

local current_time = tick()

wait(3)

print(tick()-current_time)

That would print out 3, as 3 seconds passed. You can find more stuff on it here:

Hope this helps! :slightly_smiling_face:

96 Likes

tick gets the local time. The time is in seconds passed since the unix epoch.

One way to use it is a debounce

local f do
    local last = 0
    local debounce = 1
    function f(--[[...]])
        if tick()-last >= debounce then
            last = tick()
            --...
        end
    end
end

No, wait(3) isn’t guaranteed to wait exactly 3 seconds, it is almost always going to yield for more.
wait's first return is DeltaTime, the time it yielded for, which is about what will be printed in that example.

8 Likes

You don’t know how to use tick because you aren’t supposed to just “use it”, it’s supposed to be featured in various systems. Tick can be helpful if you’re creating some kind of a system that requires a timestamp of some sort, such as debouncing or client-side cooldowns or whatever.

4 Likes

All the replies above are great, but I feel the need to add that if you’re ever doing any time management stuff, you should use os.time() as this is guaranteed to return UTC, whereas tick() can be any timezone (however: you won’t know if this is the case!)

10 Likes

local f do? That has to be the most confusing thing ever. Could you please explain it?

5 Likes

They’re two separate things. local f will declare the variable f as a local variable and do opens a new scope similarly to how you write do in a for or while loop. The code in a do-end block must finish executing before anything after it can run, just like how code won’t execute after a while loop unless it terminates or the loop is wrapped in a coroutine.

4 Likes

I’d love to see your lessons about this. I’ve never heard about this but I wanna see some further explanations and wanna see how it works.

2 Likes