YouTube2Text

Time (DateTime, os.date(), tick()) - Roblox Advanced Scripting #28 — Transcript

by BrawlDev · 6,475 words · 857 segments · language en · Watch on YouTube

Full transcript

  1. 0:00oh what is this wait for a special
  2. 0:02surprise oh I mean like there's only a
  3. 0:04little bit of time left on this thing I
  4. 0:06might as well wait and see what happens
  5. 0:07all right what's about to
  6. 0:09happen
  7. 0:13uh okay that just
  8. 0:15happened all right let's think about
  9. 0:18this for a minute if you couldn't guess
  10. 0:20from the title of this video we're going
  11. 0:21to be discussing about time and how we
  12. 0:24can Implement that inside of Roblox
  13. 0:25studio now what do I mean by time
  14. 0:28exactly well when I say time what I'm
  15. 0:30referring to is having the ability to
  16. 0:34keep track and manipulate dates and
  17. 0:37times within a Roblox game so that we
  18. 0:40can utilize them to create time-based
  19. 0:43events actions and also just record time
  20. 0:46stamps and dates and everything like
  21. 0:47that and some examples that you might
  22. 0:49see inside of Roblox games are things
  23. 0:51like keeping track of how long a player
  24. 0:54has been playing a game for or maybe
  25. 0:55other things like keeping track of
  26. 0:57certain timestamps so that when the game
  27. 1:00reaches a certain time that's when we
  28. 1:02decide to do a certain action or we
  29. 1:04decide to do a certain event because a
  30. 1:07certain time has passed and just things
  31. 1:08like that and before we start
  32. 1:10implementing I want to throw some
  33. 1:11terminology out here so that um you have
  34. 1:13a better understanding of how time works
  35. 1:16within the programming world then we
  36. 1:17will get into implementing so with that
  37. 1:19being said let me show you more of what
  38. 1:20I have for you so what you're seeing
  39. 1:22right here is UNIX Tim stamp. org I'll
  40. 1:24leave a link in the description but
  41. 1:26there's a couple things here that may
  42. 1:28seem intriguing to you and we we need to
  43. 1:30be able to understand some terminology
  44. 1:32before we start implementing time inside
  45. 1:33of Roblox studio and there's two
  46. 1:35important ones that we need to know so
  47. 1:37the first one is what's known as the
  48. 1:39Unix Epoch now the Unix Epoch is
  49. 1:43basically this date right here January
  50. 1:461st 1970 and what you're seeing up here
  51. 1:49is the number of seconds that has passed
  52. 1:53since this date right here for January
  53. 1:551st 1970 so this is a ridiculously big
  54. 1:58number because this is how many seconds
  55. 2:00that currently have been passed since
  56. 2:03this day right here and this is what's
  57. 2:05known as a Unix timestamp which is the
  58. 2:07second terminology that we need to
  59. 2:09understand so the Unix timestamp
  60. 2:11basically tells us how many seconds have
  61. 2:12passed since the Unix Epoch time which
  62. 2:15is 1970 now you might ask why is January
  63. 2:181st 1970 so significant like why does
  64. 2:21this matter to us well if we were to
  65. 2:23think back to how computers were
  66. 2:25figuring out how to implement time
  67. 2:27inside of their systems they tried
  68. 2:29picking a Time and a date that worked
  69. 2:31very well conveniently with when
  70. 2:33computers were starting to become big
  71. 2:35and more practical so they decided to
  72. 2:37choose January 1st 1970 as the base date
  73. 2:41and time to reference how long time has
  74. 2:45passed for any current date that's after
  75. 2:48this time right here so if I picked a
  76. 2:50date that was like 1980 or 1990 for
  77. 2:52January 1st or I don't know June 10 or
  78. 2:54something then it would give us a Unix
  79. 2:56timestamp of the number of seconds that
  80. 2:58have passed in between those two dates
  81. 3:00and so when we put this into perspective
  82. 3:03uh over here it says enter a timestamp
  83. 3:06so I can put in a random Unix timestamp
  84. 3:09that displays the number of seconds and
  85. 3:10it'll show me what that time is down
  86. 3:12here so I'm just going to put in like a
  87. 3:14a randomly large number like this and
  88. 3:17it's going to display down here uh in
  89. 3:18seconds that it is Saturday October 23rd
  90. 3:222004 at this specific time at the GMT
  91. 3:25time now down here it also displays my
  92. 3:27current time zone which is EDT which
  93. 3:29displays a different time obviously
  94. 3:31because depending on the time zone you
  95. 3:33choose it's going to display a different
  96. 3:35time because you know we don't use the
  97. 3:37same time zone unless we're using
  98. 3:39universal time zone but that is
  99. 3:41basically a little bit of an
  100. 3:43understanding of how time is basically
  101. 3:45used inside of Roblox Studio but also
  102. 3:48just with computer programming in
  103. 3:49general we use a number of seconds to
  104. 3:52basically tell us how much time has
  105. 3:54passed in between the date versus the
  106. 3:57Unix Epoch time now that we understand
  107. 4:00it let's Implement time inside of Roblox
  108. 4:02studio so what I'm going to do is go on
  109. 4:04the right side of server script service
  110. 4:06insert a script like this and then
  111. 4:07delete this code that's over here now
  112. 4:10Roblox has given us a data type that is
  113. 4:13actually going to be very useful for us
  114. 4:14to use in order to keep track and manage
  115. 4:17time within our Roblox game specifically
  116. 4:20with dates essentially this data type is
  117. 4:23called date time and this is how we're
  118. 4:25able to construct timestamps within our
  119. 4:28Roblox game and how we can use them to
  120. 4:30keep track of time so what I'm simply
  121. 4:32going to do is just type this I'm going
  122. 4:33to say local date time equals dat
  123. 4:40time. now open and close parenthesis so
  124. 4:42basically what this is doing is it's
  125. 4:44returning a date time that specifies how
  126. 4:48many seconds have passed from the Unix
  127. 4:51Epoch January 1st 1970 to the time that
  128. 4:55it currently is right now which is June
  129. 4:577th 2024 as of recording this video
  130. 5:00and so if I were to go down here and
  131. 5:02print the
  132. 5:03datetime object if I were to hit run
  133. 5:06then what we should see in the output is
  134. 5:07the number of seconds that have passed
  135. 5:09since January 1st 1970 which is the Unix
  136. 5:12zoch and that is one way we can
  137. 5:14construct a datetime um object there's
  138. 5:17other ways we can do it and I'm going to
  139. 5:18show you uh what the other ones are
  140. 5:20right now so I'm going to drop two lines
  141. 5:23down here and another way we can create
  142. 5:25a daytime object is by uh specifying
  143. 5:28what the unit timestamp is right off the
  144. 5:31bat like basically the number of seconds
  145. 5:33that we're going to use to create a
  146. 5:35datetime object so what we're going to
  147. 5:37do is say local timestamp equals dat
  148. 5:42time. from Unix time stamp like this and
  149. 5:47we're going to specify a number so kind
  150. 5:49of like the website that we were using
  151. 5:51that showed the the number of seconds
  152. 5:52that have passed since that period what
  153. 5:54we can do is specify a pretty massive
  154. 5:57number well actually I'm not going to
  155. 5:58start with three I'm going to start with
  156. 5:59one one and then whatever number is
  157. 6:01going to be passed here so what's going
  158. 6:04to happen is if we print timestamp then
  159. 6:06obviously if we go into the game and hit
  160. 6:08run it's just going to print the time
  161. 6:11that we specified uh within the seconds
  162. 6:14that it passed so that part's no
  163. 6:16different but that's another way we can
  164. 6:17uh create a daytime object now there's
  165. 6:20two more ways and both of them are
  166. 6:21related to each other because it relates
  167. 6:23to putting in a specific date so that
  168. 6:26the date time can basically give us the
  169. 6:28number of seconds that have pass since
  170. 6:30the Unix Epoch so we can do it either
  171. 6:33with universal time or the local time of
  172. 6:36the time zone you live in but first
  173. 6:38we're going to do uh the universal time
  174. 6:40so we're going to say local universal
  175. 6:43time equals date time. from
  176. 6:47Universal time open close parenthesis
  177. 6:50and now we're going to be given a bunch
  178. 6:52of arguments that we have to put down
  179. 6:53that specifies the date so I'm going to
  180. 6:55put in a random uh date inside of here
  181. 6:57so I'm going to say something like 2009
  182. 7:00as the year uh we can say June as the
  183. 7:03month which is six and then the day
  184. 7:04which is probably I don't know 13 or
  185. 7:06something and then the hour we're going
  186. 7:08to make it 3 hours in so it's like 3:00
  187. 7:10a.m. minutes 19 and then seconds we'll
  188. 7:13do 32 so we've specified a date here now
  189. 7:16if we were to specify local time then we
  190. 7:19can basically do the exact same thing by
  191. 7:21saying local local time equals date
  192. 7:25time. from local time open and close
  193. 7:28parentheses and we can basically just
  194. 7:30copy the same arguments we had here and
  195. 7:32put it into here now if we were to print
  196. 7:34both of these numbers out universal time
  197. 7:36and then also print the local time and
  198. 7:39if we were to go into the game and hit
  199. 7:40run then what we should see in the
  200. 7:42output are two very similar numbers but
  201. 7:46they are a little bit different there's
  202. 7:47like about three or four numbers here
  203. 7:50that are different from each other
  204. 7:52because there's a difference when it
  205. 7:53comes to choosing a time that's
  206. 7:56Universal versus a time that's local so
  207. 7:58local is like basically where you live
  208. 8:01in the world right now the time for you
  209. 8:03is going to be different for a time that
  210. 8:04lives across the world from you but
  211. 8:06universal time is going to be the time
  212. 8:08that's shared across every single time
  213. 8:10zone in the world so depending on how
  214. 8:12you use time in your game I think it it
  215. 8:15matters which one you do pick so those
  216. 8:18are some ways you can construct a
  217. 8:20datetime object inside of Roblox studio
  218. 8:22and I hope you found this part to be
  219. 8:23helpful so far okay so now let's put
  220. 8:26these timestamps to practical use I'm
  221. 8:28going to be using using two timestamps
  222. 8:31to uh calculate the elapsed time in
  223. 8:34between these two timestamps so this is
  224. 8:36basically what I'm going to be doing so
  225. 8:38I'm going to uh comment out everything
  226. 8:40that we've created up until this point
  227. 8:42and I'm going to create a start time so
  228. 8:45I'm going to say local start time equals
  229. 8:49date time. now so we're going to get the
  230. 8:52current time right now of the number of
  231. 8:55seconds that have passed since the Unix
  232. 8:57Epoch time and what what I'm going to do
  233. 9:00down here is make a Tas out weight
  234. 9:01statement for about 3 seconds let's say
  235. 9:04and now what I'm going to do is create
  236. 9:05an end time so I'm going to say local
  237. 9:08end time equals dat time. now so I'm
  238. 9:12going to do the exact same thing which
  239. 9:14is going to give me the number of
  240. 9:15seconds that have passed from the
  241. 9:17current time that this date time has
  242. 9:19been initiated or created from the Unix
  243. 9:22zoch time and so in order to calculate
  244. 9:25the difference between these two times
  245. 9:27we're going to Simply subtract the time
  246. 9:29that it took to create this daytime
  247. 9:31object and the time that it took to
  248. 9:33create this daytime object from the
  249. 9:35start so I'm going to drop two lines
  250. 9:37make a print statement down here and
  251. 9:39simply say end time minus start time so
  252. 9:44basically what we're doing is we're
  253. 9:45taking the end time and this returns a
  254. 9:48big number which is the Unix Tim stamp
  255. 9:51from the Unix Epoch time to what the
  256. 9:53time currently is right now which is
  257. 9:55going to be a big number now this one is
  258. 9:57going to be 3 seconds behind behind when
  259. 10:00this one was created so these two are
  260. 10:02going to be very similar numbers but if
  261. 10:04we subtract these numbers with each
  262. 10:06other it's going to return the number
  263. 10:08three because we're differentiating when
  264. 10:11this daytime was created versus when
  265. 10:13this time is created so if we go into
  266. 10:15the game and hit run then it's actually
  267. 10:17not going to work because what's going
  268. 10:19to happen in the output is it's going to
  269. 10:21say that it failed to perform a or it
  270. 10:24failed to perform arithmetic on daytime
  271. 10:27and that's because we can't subtract a
  272. 10:30daytime object with another daytime we
  273. 10:33have to get the time stamp itself in
  274. 10:35seconds so this is actually a property
  275. 10:37we can use by simply going to each of
  276. 10:40these datetime objects and say dot Unix
  277. 10:45time stamp like this and then we're
  278. 10:46going to do the same thing over here by
  279. 10:47saying Unix timestamp like that so now
  280. 10:51we're getting the actual seconds uh as a
  281. 10:54number and we're going to subtract these
  282. 10:55two together to get the result that we
  283. 10:56want so if we go into the game hit run
  284. 10:58then we should see in the output after
  285. 11:00about 3 seconds is going to be the
  286. 11:03number three because 3 seconds have
  287. 11:05passed since the Unix time here and the
  288. 11:09Unix time from when we started because
  289. 11:10there was a 3se second delay from when
  290. 11:12each of these daytime objects have been
  291. 11:14created I hope this part has made sense
  292. 11:16to you at least up until this point so
  293. 11:17that is basically how you calculate
  294. 11:19elapsed time using daytime objects now
  295. 11:23I'm going to show you how we can format
  296. 11:25our timestamps so that they are more
  297. 11:28readable as actual timestamps and not
  298. 11:31just a bunch of numbers that have
  299. 11:33elapsed since the Unix Epoch so there's
  300. 11:35two ways of doing it and both of them
  301. 11:37involve either using the universal time
  302. 11:40or the local time so I'm going to show
  303. 11:42you how to do both of them what I'm
  304. 11:44going to do is basically just comment
  305. 11:45out what we've had up until this point
  306. 11:48and what I'm going to do is basically
  307. 11:50just copy what we had up here with
  308. 11:52universal and local time and just paste
  309. 11:54it down here so here's what we're going
  310. 11:56to do there's a method that I want to in
  311. 11:59introduced to you to format let's say
  312. 12:01our universal time up here and that is
  313. 12:04going to be called format universal time
  314. 12:07so we're just going to put this directly
  315. 12:08inside of a print statement by saying
  316. 12:10print open close parentheses universal
  317. 12:14time colon format universal time open
  318. 12:19close parentheses now there's two
  319. 12:20arguments that are put inside of this
  320. 12:22format universal time and I feel like I
  321. 12:24need to give a little bit more of
  322. 12:26context before we start adding stuff
  323. 12:28inside of this function right here so
  324. 12:31I'm on the documentation for string
  325. 12:33formatting for date times and there's
  326. 12:35two arguments that are important here so
  327. 12:36the first one is going to be a token
  328. 12:39that basically shows what kind of format
  329. 12:41do we want from this datetime and the
  330. 12:44second argument is the language that we
  331. 12:46want to uh format this daytime for now
  332. 12:49for the sake of this tutorial we're
  333. 12:51going to be using ianus which is short
  334. 12:54for English United States and basically
  335. 12:57there's a table here that shows the
  336. 12:59tokens that we can use as our first
  337. 13:01argument so if we use a token of LT then
  338. 13:04it's basically going to give us this
  339. 13:06value right here this string and there's
  340. 13:08different ways of representing it so
  341. 13:09this just shows the time this one shows
  342. 13:12the time with seconds um which is
  343. 13:14another way of representing it with the
  344. 13:16LTS token and we can basically do that
  345. 13:18for all of these and we can choose
  346. 13:19whichever one we want so let's say for
  347. 13:22instance we're going to choose the month
  348. 13:25as the number the day and the year so
  349. 13:28all we're simply going to do is type in
  350. 13:30capital L as our token and for the
  351. 13:34language we're going to use- us so now
  352. 13:36let's go back into studio so we can
  353. 13:38implement this inside all right so I'm
  354. 13:41inside studio and what I'm going to do
  355. 13:43is in quotations I'm going to first give
  356. 13:45the format which is going to be capital
  357. 13:47L and then for the second argument it's
  358. 13:49going to be uh in all lowercase
  359. 13:52en-us so now if we were to go into the
  360. 13:55game and hit run then what we should see
  361. 13:57in the output is it's going going to be
  362. 14:00613 2009 because if we look at the date
  363. 14:03that we created for the universal time
  364. 14:05the year is indeed 2009 and the month is
  365. 14:08six which is June and then 13 is the day
  366. 14:10so we were able to format it um in the
  367. 14:13English United States language and so
  368. 14:15that is how we format our strings and we
  369. 14:18can basically do the exact same thing
  370. 14:20with our local times as well so we can
  371. 14:22just copy this paste it down here but
  372. 14:24instead of format universal time we can
  373. 14:27say format local time and this will give
  374. 14:29us a different result if we go into the
  375. 14:31game and hit run so I'm actually going
  376. 14:33to replace universal time with the local
  377. 14:35time variable as well so now if we go
  378. 14:37into the game and hit run then we should
  379. 14:38get probably the same results yeah cuz
  380. 14:41like the dates aren't really going to
  381. 14:42change that much um unless you push the
  382. 14:45time to be closer to the end of the day
  383. 14:47so I would pick a different example to
  384. 14:49to really show you the difference but
  385. 14:51you kind of get the idea at this point
  386. 14:53of how we can format our strings to be
  387. 14:55different using tokens and also using
  388. 14:57the language that we want to use to
  389. 14:59format these date timestamps rather than
  390. 15:01just showing a bunch of numbers we can
  391. 15:04actually read it
  392. 15:05properly so that's pretty much how you
  393. 15:07use datetime objects inside of Roblox
  394. 15:10Studio to help you keep track of time
  395. 15:11within your game now there are other
  396. 15:14time functions that Roblox has for us
  397. 15:16that you might find to be useful so I'm
  398. 15:18actually going to include them inside of
  399. 15:20this video and there's two of those
  400. 15:22functions the first one is being simply
  401. 15:24just time now if I go inside of the
  402. 15:27script I'm actually going to comment out
  403. 15:29everything we've made up until this
  404. 15:30point and what I'm going to do down here
  405. 15:32is just simply make a print statement
  406. 15:34and inside of here I'm just going to say
  407. 15:37time open close parenthesis now
  408. 15:39basically what time is is it Returns the
  409. 15:42number of seconds of how long the game
  410. 15:45has been running for so if I were to
  411. 15:48basically go into the game and hit run
  412. 15:51obviously what's going to be shown in
  413. 15:52the output is zero because as soon as
  414. 15:54the game starts the server is literally
  415. 15:57zero seconds old like nothing in the
  416. 15:59server has run long enough for it to be
  417. 16:01bigger than zero now if I were to go and
  418. 16:05um let's say make a task. wa for about 3
  419. 16:08seconds and then print time if you were
  420. 16:10to go into the game wait for about 3
  421. 16:11seconds and then see what the time is
  422. 16:14it's going to be this very specific
  423. 16:16number which is 1.89 something something
  424. 16:18something and that's because this task.
  425. 16:21weight statement is being ran before the
  426. 16:23game gets to keep track of the time that
  427. 16:26it took for the game to start up and run
  428. 16:29so that's why this number is going to be
  429. 16:31a little like inconsistent when you're
  430. 16:33starting the game for the first time but
  431. 16:34I would say if you want to uh keep track
  432. 16:37of how long the game's been running for
  433. 16:38or at least this specific server that
  434. 16:40started up then I would say using time
  435. 16:43can be pretty useful um in certain
  436. 16:46situations now the next one you might
  437. 16:48use is called os. clock and this is a
  438. 16:51function that Roblox has given us to use
  439. 16:54as part of their OS Library so I'm going
  440. 16:56to show you what this looks like right
  441. 16:57here I'm going to these two lines down
  442. 17:00here and I'm just going to print os.
  443. 17:03clock now basically what this does is it
  444. 17:07basically returns how much CPU time is
  445. 17:09being used in seconds and this is very
  446. 17:12useful to use if you're trying to debug
  447. 17:15and optimize scripting performance if
  448. 17:18you want to uh try and improve your
  449. 17:20scripts to make it have less of a CPU
  450. 17:23time so basically um what this would
  451. 17:25look like is if you were to just go into
  452. 17:26the game and hit run then what we should
  453. 17:29see is just an arbitrary number that
  454. 17:31shows how much CPU processing time is
  455. 17:34being used inside of Roblox Studio but
  456. 17:36we can actually use it in a certain way
  457. 17:38to help us understand how much
  458. 17:41processing is required to perform a
  459. 17:44certain complex task and so this is
  460. 17:46basically how I'm going to show it to
  461. 17:47you so let's actually comment this out
  462. 17:49and what I'm going to do is drop two
  463. 17:51lines and I'm going to have a start time
  464. 17:53so the start time is going to be local
  465. 17:55start time equals os. clock just like
  466. 17:58this and I'm also going to have an end
  467. 18:00time so I'm going to say local end time
  468. 18:03equals os. clock open and close
  469. 18:05parenthesis and then finally I'm going
  470. 18:06to have a print statement which is going
  471. 18:08to give us the difference like the time
  472. 18:10that that has elapsed between the end
  473. 18:13time and the start time so it's going to
  474. 18:15be end time minus start time just like
  475. 18:18this and that is basically all we have
  476. 18:20going so far but right now it's not
  477. 18:23really going to it's not going to be
  478. 18:24telling us much because the time that it
  479. 18:26takes to execute this line right here
  480. 18:28and the time that it takes to execute
  481. 18:30this line is going to be almost like
  482. 18:33nothing basically so if we hit run then
  483. 18:35what we should see in the output is a
  484. 18:37very very very very tiny number here
  485. 18:40with like you know and we can tell it's
  486. 18:42tiny because it's using an exponential
  487. 18:44value of-7 to the left side so that
  488. 18:46means it's a very tiny number um if we
  489. 18:49had a complicated calculation in between
  490. 18:51the start and end time then it would
  491. 18:53actually give us a more precise number
  492. 18:56to show how much CPU time it took to
  493. 18:58execute what whatever was inside of here
  494. 19:00so let's make something very complicated
  495. 19:02let's use a for Loop let's say 4 I = 1
  496. 19:06comma and then let's put in a really
  497. 19:07large number like I'm going to put like
  498. 19:10this really massive number right here
  499. 19:11and I'm going to say do just like this
  500. 19:13and what I'm also going to do is let's
  501. 19:16make an accumulator value like a sum
  502. 19:18value basically so I'm going to say
  503. 19:20local sum equals z and I'm going to set
  504. 19:24sum uh equal to whatever the sum is plus
  505. 19:28I now if you want to shorten this we can
  506. 19:30actually just say plus equals I just
  507. 19:32like this now obviously this is going to
  508. 19:34take a lot of processing power to do so
  509. 19:35I'm actually going to keep track of how
  510. 19:37big this number actually is by adding
  511. 19:38underscores and as you can see we're at
  512. 19:40100 million if we were to go into the
  513. 19:42game and hit run then what we should see
  514. 19:45is that it took 0.5 seconds to process
  515. 19:48um whatever was inside of this code
  516. 19:50right here but if we were to make this
  517. 19:52an even bigger number so if we were to
  518. 19:54say let's say 1core 0 so instead of 100
  519. 19:58million we are now reaching 1 billion so
  520. 20:01if we were to run this code then it's
  521. 20:03going to take a little bit of time it's
  522. 20:05going to be very slow for the CPU to
  523. 20:07calculate everything that's going to be
  524. 20:09inside of the script and as you can see
  525. 20:11this took 5 Seconds to calculate what
  526. 20:13was happening inside of the script
  527. 20:14rather than 0.5 seconds so I would
  528. 20:16recommend using os. clock if you want to
  529. 20:19optimize performance within your scripts
  530. 20:21so that you know how much time it takes
  531. 20:23to do certain calculations so that's
  532. 20:26another useful time function that isn't
  533. 20:29part of the daytime library that I hope
  534. 20:31you'll find to be useful inside your
  535. 20:33scripts okay so now I'm going to show
  536. 20:35you some time functions that Roblox has
  537. 20:37for us that I recommend not using and
  538. 20:41the reason I recommend not using these
  539. 20:43is because um these functions are
  540. 20:44outdated and uh and these are replaced
  541. 20:47with better functions with the ones that
  542. 20:50I've showed you to use inside of this
  543. 20:51video but just for educational purposes
  544. 20:53I'm going to show you what each of them
  545. 20:54are so that you know not to use them so
  546. 20:57I'm going to go back to my script and
  547. 20:59I'm going to uh comment out everything
  548. 21:01that's inside of here and the first one
  549. 21:03that I'm going to show you is Tick open
  550. 21:05and close parentheses now basically what
  551. 21:07tick does is it Returns the number of
  552. 21:09seconds that have passed from the
  553. 21:11current time uh and also the Unix EPO
  554. 21:14time now from what I've showed you with
  555. 21:17datetime it basically does the exact
  556. 21:19same thing with daytime. now I would say
  557. 21:22for newer work you should use daytime.
  558. 21:24now rather than using tick because it's
  559. 21:26a much better feature to use than using
  560. 21:27tick it's pretty outdated for
  561. 21:29localization reasons and also because it
  562. 21:32doesn't work compatibly with all time
  563. 21:34zones so that's like a general
  564. 21:37explanation that I can give as to not
  565. 21:39using tick so the next one I recommend
  566. 21:42not using is os. time now this is part
  567. 21:45of the OS library and basically what os.
  568. 21:48time does is it Returns the number of
  569. 21:50seconds that have passed since the Unix
  570. 21:53Epoch time kind of like tick it's a
  571. 21:56little bit better than tick but it's now
  572. 21:58been superseded with daytime. now like I
  573. 22:01mentioned earlier before so I would also
  574. 22:04say you should not use os. time in newer
  575. 22:06work but if you decide to use it those
  576. 22:08are the similarities os. time is similar
  577. 22:11with datetime.now but I would still say
  578. 22:13you should use daytime. now rather than
  579. 22:15using os. time and the next function is
  580. 22:18os. dat which is also part of the OS
  581. 22:21Library so os. creates a date for us to
  582. 22:26use but the the difference here is is
  583. 22:28that it's not wrapped in a datetime
  584. 22:30object but instead it's a simple
  585. 22:33dictionary full of key and value pairs
  586. 22:36that we can use to have a date now again
  587. 22:39we should not use this because date time
  588. 22:41provides localization which is very
  589. 22:43important when it comes to creating date
  590. 22:45times and also stamps or creating time
  591. 22:48stamps within Roblox studio so this is
  592. 22:50also not recommended and finally os.
  593. 22:54time now to be honest this one is kind
  594. 22:57of interesting because it can either go
  595. 22:59both ways of whether you think this will
  596. 23:01actually be useful or not but in my
  597. 23:02opinion I would say you don't really
  598. 23:04need to use this and the reason I say
  599. 23:06this is because basically what diff time
  600. 23:08does is it takes two date timestamps and
  601. 23:12it finds the difference between those
  602. 23:14two so basically it's the same thing as
  603. 23:16finding the elapse time between two
  604. 23:18timestamps but you can simply just do
  605. 23:20this by subtracting the Unix timestamp
  606. 23:23from one date and the Unix timestamp
  607. 23:24from another date and you get the
  608. 23:26difference of time in there so I don't
  609. 23:27really see what the point of having this
  610. 23:29function is I could be proven wrong but
  611. 23:31from what I've been able to experiment I
  612. 23:34don't think there's really any reason to
  613. 23:35use this and also because I don't think
  614. 23:38it's compatible with daytime objects as
  615. 23:40well so I would give a recommendation to
  616. 23:42not use diff time as well from the OS
  617. 23:44Library so with these four functions you
  618. 23:47should not use them in your newer Roblox
  619. 23:49games but instead use the functions that
  620. 23:52I've provided to you from the other
  621. 23:53parts of this tutorial so date times and
  622. 23:57also OS clock and normal
  623. 24:01time so for today's challenge I want you
  624. 24:04to recreate this um expiration Code
  625. 24:07system right here where basically what's
  626. 24:08going to happen is when we insert a code
  627. 24:11that either passes an expiration date or
  628. 24:14has not passed an expiration date that's
  629. 24:16going to determine if this code is
  630. 24:18redeemable so when we select this text
  631. 24:21box right here um we can basically put
  632. 24:23in whatever text we want so the valid
  633. 24:25code is just simply called valid code
  634. 24:29just like this and when we hit our Enter
  635. 24:31key then it's going to say on the bottom
  636. 24:33of the screen whether the code has been
  637. 24:35redeemed or if the code is expired or is
  638. 24:39um invalid so I'm going to show you what
  639. 24:40this looks like on the opposite end
  640. 24:43[Music]
  641. 24:43[Applause]
  642. 24:47[Music]
  643. 24:50[Applause]
  644. 24:52now all right so I changed the
  645. 24:53expiration date so now if we select so
  646. 24:57now if we type in Val code once again
  647. 24:59then it's going to say that the code is
  648. 25:00expired or invalid because it passed the
  649. 25:03expiration date so that's what I want
  650. 25:05you to do with this challenge I want you
  651. 25:07to use timestamps to be able to give an
  652. 25:08expiration date and check if the current
  653. 25:11time right now has either passed the
  654. 25:13expiration date or has not pass the
  655. 25:14expiration date and I know a tool that
  656. 25:16will help you with this challenge I'm
  657. 25:18going to show you this right
  658. 25:19now okay so I'm on this website called
  659. 25:22UC time.net and basically what this does
  660. 25:25is it shows me what the current UC time
  661. 25:27of of the day is right now because I
  662. 25:30think if we're going to be uh creating
  663. 25:32an expiration system I think we should
  664. 25:34be using UTC time that's more General
  665. 25:36across the entire world versus our local
  666. 25:39time so this is my local time right here
  667. 25:41but what we're interested in is this
  668. 25:43time right here so I want you to use
  669. 25:45this time like whatever it shows right
  670. 25:47here to determine if it's an expired
  671. 25:49code or if it's a redeemable code
  672. 25:51currently right now so you can use this
  673. 25:54if if this will help you but this is
  674. 25:55something that I'll leave for you in the
  675. 25:56description if you want to use it so
  676. 25:57with that being said I'm going to let
  677. 25:59you off to try this Challenge on your
  678. 26:01own uh come back once you're done so
  679. 26:02that we can go through this together all
  680. 26:04right so what I'm going to do is go on
  681. 26:06the right side of starter guy and I'm
  682. 26:08going to insert a screen guy and then
  683. 26:10inside of here I'm going to create a
  684. 26:13text box like this I'm going to take the
  685. 26:15text box I'm going to scale it up just a
  686. 26:17little bit and then move it towards the
  687. 26:19center kind of like right here oh I
  688. 26:21don't want to type anything in there um
  689. 26:23and so now what I'm going to do is
  690. 26:25scroll down here and then create a
  691. 26:27placeholder text which I think I
  692. 26:29remember it was like uh type code here
  693. 26:32dot dot dot something like that and I'm
  694. 26:34also going to make the text scale just
  695. 26:37like this so now we have our text box
  696. 26:39and now we're going to make this
  697. 26:40functional so we're going to uh hit the
  698. 26:41plus sign and we're going to insert a
  699. 26:43local script inside of the text box
  700. 26:45going to delete this and first thing I'm
  701. 26:46going to do is make reference to the
  702. 26:47text box by saying local textbox equal
  703. 26:50script. parent and then I'm going to say
  704. 26:54uh two pieces of information that we are
  705. 26:56going to need the first one is going
  706. 26:58going to be what the code actually is
  707. 27:00and the second one is going to be the
  708. 27:02expiration date timestamp that we're
  709. 27:04going to create to let us know what the
  710. 27:06expiration date actually is so the first
  711. 27:07thing I'm going to do is create a code
  712. 27:10uh string by saying local valid code
  713. 27:13equals in quotations we're just going to
  714. 27:15say valid code as the code itself and
  715. 27:18then we're going to create an expiration
  716. 27:19date we're going to say local expiration
  717. 27:22date equals and we're going to use
  718. 27:24datetime objects by saying datetime
  719. 27:28from Universal time because we're going
  720. 27:31to be using a universal time not a local
  721. 27:33time and now we're going to uh fill out
  722. 27:35our arguments by saying 2024 the month
  723. 27:38being June the day being 7 and the hour
  724. 27:42being 21 and then minutes and seconds we
  725. 27:44can just make that zero now again I
  726. 27:46think you should change this to
  727. 27:48something that's more relevant to what
  728. 27:50your time currently is or at least a
  729. 27:52time that's closer to where your time is
  730. 27:55right now versus what I have over here
  731. 27:57okay so now what we're going to do is
  732. 27:59use an event to tell uh whether the user
  733. 28:03has pressed enter on their keyboard um
  734. 28:06after they lose focus of this text box
  735. 28:08so there's actually an event for text
  736. 28:10box simply just called Focus loss that
  737. 28:12we're going to use and if you want a
  738. 28:14more in-depth guide on using uh
  739. 28:16textboxes then I'm going to leave a link
  740. 28:18for that for you right here to watch
  741. 28:19essentially what we're going to do is
  742. 28:21say text box. Focus lost colon connect
  743. 28:25function open close parentheses enter
  744. 28:27and and there's going to be one argument
  745. 28:29here that we're actually going to use
  746. 28:31and it's a Boolean that basically says
  747. 28:33if the player has pressed the enter key
  748. 28:35as it's being selected so we're just
  749. 28:37going to say enter pressed like this and
  750. 28:39we're going to make a simple check to
  751. 28:41see if the player has uh pressed their
  752. 28:43Enter key then we're going to make two
  753. 28:46checks so the first check is going to be
  754. 28:49whether the current date has passed or
  755. 28:52has not passed the expiration date and
  756. 28:54we're also going to check if the code
  757. 28:56that the user entered is valid and it
  758. 28:58matches the code that's up here so what
  759. 29:00we're going to do is say if and we need
  760. 29:03to calculate how we're going to
  761. 29:05determine if this date has been expired
  762. 29:08or not so in parenthesis we're going to
  763. 29:11first put in the expiration date and as
  764. 29:13you know the expiration date is the
  765. 29:15number of seconds that have passed since
  766. 29:17the Unix Epoch and if we basically take
  767. 29:20this date and we subtract that with the
  768. 29:22current date so we basically have a
  769. 29:26current date variable probably somewhere
  770. 29:27over here so before this I'm actually
  771. 29:29going to say local current date equals
  772. 29:33date. now open close parentheses or
  773. 29:35sorry uh I meant to say date time and so
  774. 29:40now what we're going to do is say
  775. 29:41current date if this is greater than
  776. 29:45zero so basically if we have the
  777. 29:47expiration date so this date that's like
  778. 29:49right over here if the time uh elapsed
  779. 29:52since Epoch if we subtract that with the
  780. 29:55current date and if the number number
  781. 29:58that we get is going to be greater than
  782. 29:59zero meaning if there is time that is
  783. 30:04meaning that if there is a discrepancy
  784. 30:05in time between the expiration date and
  785. 30:07the current date that means the the time
  786. 30:10has already passed I hope this
  787. 30:11calculation has made a little bit of
  788. 30:12sense to you in how we understand where
  789. 30:14this actually came from and how this is
  790. 30:16relevant to uh what we're trying to
  791. 30:18calculate up here so this determines if
  792. 30:21the the date is past the expiration and
  793. 30:23now what we're going to do is also check
  794. 30:25if the code is valid so we're going to
  795. 30:26say string lower and the reason we're
  796. 30:29using this lower method inside of
  797. 30:31strings because we want to take whatever
  798. 30:33uh text is inside of this text box and
  799. 30:35see if it matches this one perfectly
  800. 30:38like exactly this string right here so
  801. 30:41inside of here we're just going to say
  802. 30:43uh text box. text like this and then
  803. 30:47we're going to make a print statement
  804. 30:48saying code has been uh redeemed
  805. 30:52successfully like this and uh if it
  806. 30:55fails then we're just going to print
  807. 30:57saying um code uh is
  808. 31:02expired or invalid and there we go we
  809. 31:06basically have our script right here
  810. 31:07that determines um if the code is
  811. 31:10expired and if the code is actually
  812. 31:11valid so now if we go into the game hit
  813. 31:13test and hit play and see this result in
  814. 31:15game then what we should see is if we
  815. 31:17type in valid code like this oh sorry I
  816. 31:21forgot to do one thing so we actually
  817. 31:22have to go back and specify that we're
  818. 31:24not trying to operate on datetime
  819. 31:27objects but we want to get the Unix
  820. 31:29timestamp as a number for the expiration
  821. 31:32date and the current date so my
  822. 31:35apologies on that now if we go into the
  823. 31:36game and see this for ourselves then uh
  824. 31:39what we should see is if we enter the
  825. 31:40code right here by saying valid code
  826. 31:42like this then it's going to say the
  827. 31:44code is expired or invalid but if we go
  828. 31:46into the game and we change this hour
  829. 31:49from 21 to 22 because right now the
  830. 31:52current hour for me as of recording this
  831. 31:54video is 21 if I change this to 22 then
  832. 31:57it's going to say that the code has been
  833. 31:58redeemed successfully and of course I
  834. 32:00can also add something invalid to this
  835. 32:02um and I I guess it's still going to say
  836. 32:04the code has been redeemed successfully
  837. 32:06um weird okay I guess like that part
  838. 32:08doesn't really matter too much what
  839. 32:09matters more is we were able to uh
  840. 32:12calculate successfully whether this uh
  841. 32:14whether this code has been expired or if
  842. 32:17it has not been expired using the
  843. 32:19daytime objects so I hope um this has
  844. 32:21given you insight into adding this
  845. 32:23inside of Roblox Studio to keep track of
  846. 32:25time and also all these other things
  847. 32:27that you can add inside of studio with
  848. 32:30daytime and all these other time
  849. 32:31functions that I explained to you in
  850. 32:33this episode so if you found this to be
  851. 32:34helpful be sure to leave a like on this
  852. 32:36video and if you'd like to see an
  853. 32:37in-depth tutorial on textboxes then I
  854. 32:40will have a link to that for you right
  855. 32:42here to watch so go ahead and do that if
  856. 32:44you haven't already and I will see you
  857. 32:45in the next episode take care

About this transcript

This page contains the full transcript of Time (DateTime, os.date(), tick()) - Roblox Advanced Scripting #28 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 6,475 words across 857 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.