What is tick()?
tick() is a Roblox built-in function, it returns how much time has passed in seconds since the UNIX epoch, which is January 1st, 1970 12:00:00 A.M.
The UNIX epoch, can be considered as the starting point for Unix time, the initial time, so it is 0.
For example, say the Unix time is January 1st, 1970 12:01:00 A.M. , tick() would return 60 (seconds).
Examples of tick() usage:
With tick() , you can see how much time has passed from a starting point you chose. One good example is, printing something after a specific amount of time.
First we would assign a variable to tick() , in this case t would be the initial time, your "starting point".
Keep in mind that t would be assigned to tick() as soon as you run the code in Studio.
local t = tick()
Then we would check if 5 seconds have passed, how would we do that you may ask? Well by checking if the difference between the current Unix time and the initial Unix time t has reached 5 seconds, which means we would use subtraction.
local t = tick() if tick()-t == 5 then print("5 seconds have passed.") end
And the following output would be:
Nothing? Well that's because the code only ran one time, and only checked for the condition one time, so obviously by that 5 seconds wouldn't have passed, then.. How are we going to do this? Oh yeah! Loops!
For this example we would use a while loop, hopefully you know what a while loop is and understand its concept!
With the while loop, the code will become the following:
local t = tick() while true do if tick()-t==5 then print("5 seconds have passed.") end wait() end
and the output would be:
Still nothing? Well that's because tick()-t isn't a whole number, an integer like 1, 2, 6, etc....
So we should do math.floor(tick()-t) instead of tick()-t and check if that equals 5!
Then the code will be:
local t = tick() while true do if math.floor(tick()-t)==5 then print("5 seconds have passed.") end wait() end
And the output after 5 seconds after running the code would be:
> 5 seconds have passed.
Finally! You did it!
Note: you can also use RunService instad of the while loop, which in my opinion would be more efficient.
Another example of using tick() is checking for a double-click!
We would as usual assign a variable "t" to tick() .
local t = tick()
And assuming this code is a LocalScript you can get the mouse instance with Player:GetMouse()
local Player = game.Players.LocalPlayer local t = tick() local mouse = Player:GetMouse()
Of course to check for a mouse click (a left mouse button click in this case), we would use the Button1Down event, again, hopefully you know about events.
local Player = game.Players.LocalPlayer local t = tick() local mouse = Player:GetMouse() mouse.Button1Down:Connect(function() if tick()-t<0.4 then print("Double click!") end t = tick() end)
Two things you would notice are, why did I not put math.floor() ? and why did I put such a small number? Well, before answering the first question, let me answer the second one first, since I put a small number like 0.4, because a double click can happen in a really short amount of time, math.floor() would not work obviously because 0.5 is not an integer.
Anyway, the output would be, if you double-click:
> Double click!
And there you have it, you got some little insight about tick() and some examples of its usage! Now obviously, tick() can be used to do other things! So have fun discovering!
Thank you for your time!
Easy Written By Slazai
See Slazai's profile on Roblox