Simple to learn notes on time() / tick(), For animation or movement scripts and so forth

— time() function below —
The function time() is quite similar to the function tick(). However, you can tell that tick() gives a huge number like “1654614065”, but time() give a simple number that starts from 0 and counts up from there. time() is also much more diverse in how you can use it. Here is some code to show for it.

while wait() do---{While Loop} "v"
	print(time()) -- This line will print a timer that will start at 0 and continue to go up, at game start. -- this is good for making timers for games
	
	print(os.time(), " Os Time") --  This line of code >{os.time()} will print the amount of secounds have passed since 1970~, this is good for telling the date
	
	--Below here is things to do with the term "os" in script--
	print(os.date(), " Os Date") -- This line of code >{os.date()} is much better on telling what day/mouth/year it is in real time in script,-- This is much more recommended to use for telling the date instead of "os.time()"
end

More about what the term “os” stands for and what it does can be found here.
https://developer.roblox.com/en-us/api-reference/lua-docs/os

This function time() can also be made into a “loop timer” just like the function tick(), as shown below.

local Z = 5 --- This stands for how fast the timer will go.
local X = 10 --- This stands for the max number the timer goes to before reseting again.

while wait() do --{While Loop} "v"
	print(Z*time() % X) ---This is a visual line that will show how the function {Z*time() % X} works.
	---{Try changing the variables X & Z to change how the timer functions!}
end

.
.
.
.
— tick() function below — Apparently it is old and is soon to be deprecated. —old text btw.
—Most of this code below works the same as time().

So I’ve just recently learned what the function tick() % --Variable can do when it comes to moving a part in Roblox Studio, and would like to give a quick and easy way to understand how this functions works and how it can be useful ‘Since I don’t see any threads about this topic in particular :smile:’.

So first I’ll tell you in a simple way about what tick() is if you don’t know already, “Feel free to try some of the codes shown in your own studio!”.
First and foremost tick() is a timer that has been counting the seconds since 1970, “Quite Useless info but that’s what tick() mainly is” But this code should show you what it does in real-time, have a go at it!

local PassedTime = tick() ---This is getting a current time of when the script starts running and will continue to count as the script runs.

while wait() do ---{While Loop}"v"
	local RenderedTimePassed = tick() - PassedTime ---This will get the old-timer
 that you started > {PassedTime} and minus it by the new time you just set > {RenderedTimePassed}

	print(RenderedTimePassed) ---Now you will Print that "RenderedTimePassed" which should give you a continuing timer of how long it's been since the script started.
	--This can be used in many ways: a timer for a clock, a date checker, and so on
end

Now what you just read is basically just a timer that continues to count until the script/code has stopped, quite useful for some cases. Now what I want to show you is the same thing but it resets automatically :robot:. This can be used for a swaying motion just from script code, or even something moving back and forth or on the more basic side of things, just a timer that reset at the time you set it to do so.

Now here’s how it works and what to do to achieve this with the tick() function

while wait() do --{While Loop} "v"
	
	local LoopTimer = tick() % 10 --- This line of code is the same as tick(), but it reduces the base number back down to 0 and counts up from there {v}
	-- and the number {10} is how high this time will count up to before reseting back to 0 and continuing the same cycle.
	
	local SpeededUpLoopTimer = 6*tick() % 10 -- However you can speed up how fast this timer goes, by putting a variable before the tick() timer. As show in the function" { X*tick() % Z }
	-- try changing the number 6 to speed up or slow down the timer!
	
	print(SpeededUpLoopTimer) --- Now we will print the line of code just for visual in the command-bar.
	
end

Anyways, that was my basic but helpful version of the tick() Function. If you have any questions or problems with how I adressed the topic, feel free to type it out

5 Likes

tick is deprecated, use time() instead.

You can still use tick. os.time returns only an integer so it won’t suit this use case. os.clock is more suitable.

Yes but its deprecated. also i dont mean os.time but time() itself


Where’s the “Deprecated” tag?

If it’s deprecated, it’ll show this;

Or this on the main page;

It’s soon to be deprecated from what I heard

Where did you hear that? and why would they deprecate tick?

2 Likes

Because its slightly innacurate. I believe, use time().

cc : @Judgy_Oreo

Thanks for letting me know, I had no idea that it will be deprecated. I wish they already marked it as deprecated so that I don’t write my entire new codebase with tick.

What about os.clock(), should time() be utilized over that?

os.clock was mainly for benchmarking and expensive. which is why i prefer time()

I never knew about this. Would my script still work with time()?
I’ve never used the function time before.

1 Like

time is similar to tick. You should be fine.

In addition to this, if you want the time since the epoch as an integer, you can use os.time() (which I’m pretty sure is locked to a certain timezone, so you’re better off with os.clock(), time() and the DateTime class