Time (DateTime, os.date(), tick()) - Roblox Advanced Scripting #28 — Transcript
Full transcript
- 0:00oh what is this wait for a special
- 0:02surprise oh I mean like there's only a
- 0:04little bit of time left on this thing I
- 0:06might as well wait and see what happens
- 0:07all right what's about to
- 0:09happen
- 0:13uh okay that just
- 0:15happened all right let's think about
- 0:18this for a minute if you couldn't guess
- 0:20from the title of this video we're going
- 0:21to be discussing about time and how we
- 0:24can Implement that inside of Roblox
- 0:25studio now what do I mean by time
- 0:28exactly well when I say time what I'm
- 0:30referring to is having the ability to
- 0:34keep track and manipulate dates and
- 0:37times within a Roblox game so that we
- 0:40can utilize them to create time-based
- 0:43events actions and also just record time
- 0:46stamps and dates and everything like
- 0:47that and some examples that you might
- 0:49see inside of Roblox games are things
- 0:51like keeping track of how long a player
- 0:54has been playing a game for or maybe
- 0:55other things like keeping track of
- 0:57certain timestamps so that when the game
- 1:00reaches a certain time that's when we
- 1:02decide to do a certain action or we
- 1:04decide to do a certain event because a
- 1:07certain time has passed and just things
- 1:08like that and before we start
- 1:10implementing I want to throw some
- 1:11terminology out here so that um you have
- 1:13a better understanding of how time works
- 1:16within the programming world then we
- 1:17will get into implementing so with that
- 1:19being said let me show you more of what
- 1:20I have for you so what you're seeing
- 1:22right here is UNIX Tim stamp. org I'll
- 1:24leave a link in the description but
- 1:26there's a couple things here that may
- 1:28seem intriguing to you and we we need to
- 1:30be able to understand some terminology
- 1:32before we start implementing time inside
- 1:33of Roblox studio and there's two
- 1:35important ones that we need to know so
- 1:37the first one is what's known as the
- 1:39Unix Epoch now the Unix Epoch is
- 1:43basically this date right here January
- 1:461st 1970 and what you're seeing up here
- 1:49is the number of seconds that has passed
- 1:53since this date right here for January
- 1:551st 1970 so this is a ridiculously big
- 1:58number because this is how many seconds
- 2:00that currently have been passed since
- 2:03this day right here and this is what's
- 2:05known as a Unix timestamp which is the
- 2:07second terminology that we need to
- 2:09understand so the Unix timestamp
- 2:11basically tells us how many seconds have
- 2:12passed since the Unix Epoch time which
- 2:15is 1970 now you might ask why is January
- 2:181st 1970 so significant like why does
- 2:21this matter to us well if we were to
- 2:23think back to how computers were
- 2:25figuring out how to implement time
- 2:27inside of their systems they tried
- 2:29picking a Time and a date that worked
- 2:31very well conveniently with when
- 2:33computers were starting to become big
- 2:35and more practical so they decided to
- 2:37choose January 1st 1970 as the base date
- 2:41and time to reference how long time has
- 2:45passed for any current date that's after
- 2:48this time right here so if I picked a
- 2:50date that was like 1980 or 1990 for
- 2:52January 1st or I don't know June 10 or
- 2:54something then it would give us a Unix
- 2:56timestamp of the number of seconds that
- 2:58have passed in between those two dates
- 3:00and so when we put this into perspective
- 3:03uh over here it says enter a timestamp
- 3:06so I can put in a random Unix timestamp
- 3:09that displays the number of seconds and
- 3:10it'll show me what that time is down
- 3:12here so I'm just going to put in like a
- 3:14a randomly large number like this and
- 3:17it's going to display down here uh in
- 3:18seconds that it is Saturday October 23rd
- 3:222004 at this specific time at the GMT
- 3:25time now down here it also displays my
- 3:27current time zone which is EDT which
- 3:29displays a different time obviously
- 3:31because depending on the time zone you
- 3:33choose it's going to display a different
- 3:35time because you know we don't use the
- 3:37same time zone unless we're using
- 3:39universal time zone but that is
- 3:41basically a little bit of an
- 3:43understanding of how time is basically
- 3:45used inside of Roblox Studio but also
- 3:48just with computer programming in
- 3:49general we use a number of seconds to
- 3:52basically tell us how much time has
- 3:54passed in between the date versus the
- 3:57Unix Epoch time now that we understand
- 4:00it let's Implement time inside of Roblox
- 4:02studio so what I'm going to do is go on
- 4:04the right side of server script service
- 4:06insert a script like this and then
- 4:07delete this code that's over here now
- 4:10Roblox has given us a data type that is
- 4:13actually going to be very useful for us
- 4:14to use in order to keep track and manage
- 4:17time within our Roblox game specifically
- 4:20with dates essentially this data type is
- 4:23called date time and this is how we're
- 4:25able to construct timestamps within our
- 4:28Roblox game and how we can use them to
- 4:30keep track of time so what I'm simply
- 4:32going to do is just type this I'm going
- 4:33to say local date time equals dat
- 4:40time. now open and close parenthesis so
- 4:42basically what this is doing is it's
- 4:44returning a date time that specifies how
- 4:48many seconds have passed from the Unix
- 4:51Epoch January 1st 1970 to the time that
- 4:55it currently is right now which is June
- 4:577th 2024 as of recording this video
- 5:00and so if I were to go down here and
- 5:02print the
- 5:03datetime object if I were to hit run
- 5:06then what we should see in the output is
- 5:07the number of seconds that have passed
- 5:09since January 1st 1970 which is the Unix
- 5:12zoch and that is one way we can
- 5:14construct a datetime um object there's
- 5:17other ways we can do it and I'm going to
- 5:18show you uh what the other ones are
- 5:20right now so I'm going to drop two lines
- 5:23down here and another way we can create
- 5:25a daytime object is by uh specifying
- 5:28what the unit timestamp is right off the
- 5:31bat like basically the number of seconds
- 5:33that we're going to use to create a
- 5:35datetime object so what we're going to
- 5:37do is say local timestamp equals dat
- 5:42time. from Unix time stamp like this and
- 5:47we're going to specify a number so kind
- 5:49of like the website that we were using
- 5:51that showed the the number of seconds
- 5:52that have passed since that period what
- 5:54we can do is specify a pretty massive
- 5:57number well actually I'm not going to
- 5:58start with three I'm going to start with
- 5:59one one and then whatever number is
- 6:01going to be passed here so what's going
- 6:04to happen is if we print timestamp then
- 6:06obviously if we go into the game and hit
- 6:08run it's just going to print the time
- 6:11that we specified uh within the seconds
- 6:14that it passed so that part's no
- 6:16different but that's another way we can
- 6:17uh create a daytime object now there's
- 6:20two more ways and both of them are
- 6:21related to each other because it relates
- 6:23to putting in a specific date so that
- 6:26the date time can basically give us the
- 6:28number of seconds that have pass since
- 6:30the Unix Epoch so we can do it either
- 6:33with universal time or the local time of
- 6:36the time zone you live in but first
- 6:38we're going to do uh the universal time
- 6:40so we're going to say local universal
- 6:43time equals date time. from
- 6:47Universal time open close parenthesis
- 6:50and now we're going to be given a bunch
- 6:52of arguments that we have to put down
- 6:53that specifies the date so I'm going to
- 6:55put in a random uh date inside of here
- 6:57so I'm going to say something like 2009
- 7:00as the year uh we can say June as the
- 7:03month which is six and then the day
- 7:04which is probably I don't know 13 or
- 7:06something and then the hour we're going
- 7:08to make it 3 hours in so it's like 3:00
- 7:10a.m. minutes 19 and then seconds we'll
- 7:13do 32 so we've specified a date here now
- 7:16if we were to specify local time then we
- 7:19can basically do the exact same thing by
- 7:21saying local local time equals date
- 7:25time. from local time open and close
- 7:28parentheses and we can basically just
- 7:30copy the same arguments we had here and
- 7:32put it into here now if we were to print
- 7:34both of these numbers out universal time
- 7:36and then also print the local time and
- 7:39if we were to go into the game and hit
- 7:40run then what we should see in the
- 7:42output are two very similar numbers but
- 7:46they are a little bit different there's
- 7:47like about three or four numbers here
- 7:50that are different from each other
- 7:52because there's a difference when it
- 7:53comes to choosing a time that's
- 7:56Universal versus a time that's local so
- 7:58local is like basically where you live
- 8:01in the world right now the time for you
- 8:03is going to be different for a time that
- 8:04lives across the world from you but
- 8:06universal time is going to be the time
- 8:08that's shared across every single time
- 8:10zone in the world so depending on how
- 8:12you use time in your game I think it it
- 8:15matters which one you do pick so those
- 8:18are some ways you can construct a
- 8:20datetime object inside of Roblox studio
- 8:22and I hope you found this part to be
- 8:23helpful so far okay so now let's put
- 8:26these timestamps to practical use I'm
- 8:28going to be using using two timestamps
- 8:31to uh calculate the elapsed time in
- 8:34between these two timestamps so this is
- 8:36basically what I'm going to be doing so
- 8:38I'm going to uh comment out everything
- 8:40that we've created up until this point
- 8:42and I'm going to create a start time so
- 8:45I'm going to say local start time equals
- 8:49date time. now so we're going to get the
- 8:52current time right now of the number of
- 8:55seconds that have passed since the Unix
- 8:57Epoch time and what what I'm going to do
- 9:00down here is make a Tas out weight
- 9:01statement for about 3 seconds let's say
- 9:04and now what I'm going to do is create
- 9:05an end time so I'm going to say local
- 9:08end time equals dat time. now so I'm
- 9:12going to do the exact same thing which
- 9:14is going to give me the number of
- 9:15seconds that have passed from the
- 9:17current time that this date time has
- 9:19been initiated or created from the Unix
- 9:22zoch time and so in order to calculate
- 9:25the difference between these two times
- 9:27we're going to Simply subtract the time
- 9:29that it took to create this daytime
- 9:31object and the time that it took to
- 9:33create this daytime object from the
- 9:35start so I'm going to drop two lines
- 9:37make a print statement down here and
- 9:39simply say end time minus start time so
- 9:44basically what we're doing is we're
- 9:45taking the end time and this returns a
- 9:48big number which is the Unix Tim stamp
- 9:51from the Unix Epoch time to what the
- 9:53time currently is right now which is
- 9:55going to be a big number now this one is
- 9:57going to be 3 seconds behind behind when
- 10:00this one was created so these two are
- 10:02going to be very similar numbers but if
- 10:04we subtract these numbers with each
- 10:06other it's going to return the number
- 10:08three because we're differentiating when
- 10:11this daytime was created versus when
- 10:13this time is created so if we go into
- 10:15the game and hit run then it's actually
- 10:17not going to work because what's going
- 10:19to happen in the output is it's going to
- 10:21say that it failed to perform a or it
- 10:24failed to perform arithmetic on daytime
- 10:27and that's because we can't subtract a
- 10:30daytime object with another daytime we
- 10:33have to get the time stamp itself in
- 10:35seconds so this is actually a property
- 10:37we can use by simply going to each of
- 10:40these datetime objects and say dot Unix
- 10:45time stamp like this and then we're
- 10:46going to do the same thing over here by
- 10:47saying Unix timestamp like that so now
- 10:51we're getting the actual seconds uh as a
- 10:54number and we're going to subtract these
- 10:55two together to get the result that we
- 10:56want so if we go into the game hit run
- 10:58then we should see in the output after
- 11:00about 3 seconds is going to be the
- 11:03number three because 3 seconds have
- 11:05passed since the Unix time here and the
- 11:09Unix time from when we started because
- 11:10there was a 3se second delay from when
- 11:12each of these daytime objects have been
- 11:14created I hope this part has made sense
- 11:16to you at least up until this point so
- 11:17that is basically how you calculate
- 11:19elapsed time using daytime objects now
- 11:23I'm going to show you how we can format
- 11:25our timestamps so that they are more
- 11:28readable as actual timestamps and not
- 11:31just a bunch of numbers that have
- 11:33elapsed since the Unix Epoch so there's
- 11:35two ways of doing it and both of them
- 11:37involve either using the universal time
- 11:40or the local time so I'm going to show
- 11:42you how to do both of them what I'm
- 11:44going to do is basically just comment
- 11:45out what we've had up until this point
- 11:48and what I'm going to do is basically
- 11:50just copy what we had up here with
- 11:52universal and local time and just paste
- 11:54it down here so here's what we're going
- 11:56to do there's a method that I want to in
- 11:59introduced to you to format let's say
- 12:01our universal time up here and that is
- 12:04going to be called format universal time
- 12:07so we're just going to put this directly
- 12:08inside of a print statement by saying
- 12:10print open close parentheses universal
- 12:14time colon format universal time open
- 12:19close parentheses now there's two
- 12:20arguments that are put inside of this
- 12:22format universal time and I feel like I
- 12:24need to give a little bit more of
- 12:26context before we start adding stuff
- 12:28inside of this function right here so
- 12:31I'm on the documentation for string
- 12:33formatting for date times and there's
- 12:35two arguments that are important here so
- 12:36the first one is going to be a token
- 12:39that basically shows what kind of format
- 12:41do we want from this datetime and the
- 12:44second argument is the language that we
- 12:46want to uh format this daytime for now
- 12:49for the sake of this tutorial we're
- 12:51going to be using ianus which is short
- 12:54for English United States and basically
- 12:57there's a table here that shows the
- 12:59tokens that we can use as our first
- 13:01argument so if we use a token of LT then
- 13:04it's basically going to give us this
- 13:06value right here this string and there's
- 13:08different ways of representing it so
- 13:09this just shows the time this one shows
- 13:12the time with seconds um which is
- 13:14another way of representing it with the
- 13:16LTS token and we can basically do that
- 13:18for all of these and we can choose
- 13:19whichever one we want so let's say for
- 13:22instance we're going to choose the month
- 13:25as the number the day and the year so
- 13:28all we're simply going to do is type in
- 13:30capital L as our token and for the
- 13:34language we're going to use- us so now
- 13:36let's go back into studio so we can
- 13:38implement this inside all right so I'm
- 13:41inside studio and what I'm going to do
- 13:43is in quotations I'm going to first give
- 13:45the format which is going to be capital
- 13:47L and then for the second argument it's
- 13:49going to be uh in all lowercase
- 13:52en-us so now if we were to go into the
- 13:55game and hit run then what we should see
- 13:57in the output is it's going going to be
- 14:00613 2009 because if we look at the date
- 14:03that we created for the universal time
- 14:05the year is indeed 2009 and the month is
- 14:08six which is June and then 13 is the day
- 14:10so we were able to format it um in the
- 14:13English United States language and so
- 14:15that is how we format our strings and we
- 14:18can basically do the exact same thing
- 14:20with our local times as well so we can
- 14:22just copy this paste it down here but
- 14:24instead of format universal time we can
- 14:27say format local time and this will give
- 14:29us a different result if we go into the
- 14:31game and hit run so I'm actually going
- 14:33to replace universal time with the local
- 14:35time variable as well so now if we go
- 14:37into the game and hit run then we should
- 14:38get probably the same results yeah cuz
- 14:41like the dates aren't really going to
- 14:42change that much um unless you push the
- 14:45time to be closer to the end of the day
- 14:47so I would pick a different example to
- 14:49to really show you the difference but
- 14:51you kind of get the idea at this point
- 14:53of how we can format our strings to be
- 14:55different using tokens and also using
- 14:57the language that we want to use to
- 14:59format these date timestamps rather than
- 15:01just showing a bunch of numbers we can
- 15:04actually read it
- 15:05properly so that's pretty much how you
- 15:07use datetime objects inside of Roblox
- 15:10Studio to help you keep track of time
- 15:11within your game now there are other
- 15:14time functions that Roblox has for us
- 15:16that you might find to be useful so I'm
- 15:18actually going to include them inside of
- 15:20this video and there's two of those
- 15:22functions the first one is being simply
- 15:24just time now if I go inside of the
- 15:27script I'm actually going to comment out
- 15:29everything we've made up until this
- 15:30point and what I'm going to do down here
- 15:32is just simply make a print statement
- 15:34and inside of here I'm just going to say
- 15:37time open close parenthesis now
- 15:39basically what time is is it Returns the
- 15:42number of seconds of how long the game
- 15:45has been running for so if I were to
- 15:48basically go into the game and hit run
- 15:51obviously what's going to be shown in
- 15:52the output is zero because as soon as
- 15:54the game starts the server is literally
- 15:57zero seconds old like nothing in the
- 15:59server has run long enough for it to be
- 16:01bigger than zero now if I were to go and
- 16:05um let's say make a task. wa for about 3
- 16:08seconds and then print time if you were
- 16:10to go into the game wait for about 3
- 16:11seconds and then see what the time is
- 16:14it's going to be this very specific
- 16:16number which is 1.89 something something
- 16:18something and that's because this task.
- 16:21weight statement is being ran before the
- 16:23game gets to keep track of the time that
- 16:26it took for the game to start up and run
- 16:29so that's why this number is going to be
- 16:31a little like inconsistent when you're
- 16:33starting the game for the first time but
- 16:34I would say if you want to uh keep track
- 16:37of how long the game's been running for
- 16:38or at least this specific server that
- 16:40started up then I would say using time
- 16:43can be pretty useful um in certain
- 16:46situations now the next one you might
- 16:48use is called os. clock and this is a
- 16:51function that Roblox has given us to use
- 16:54as part of their OS Library so I'm going
- 16:56to show you what this looks like right
- 16:57here I'm going to these two lines down
- 17:00here and I'm just going to print os.
- 17:03clock now basically what this does is it
- 17:07basically returns how much CPU time is
- 17:09being used in seconds and this is very
- 17:12useful to use if you're trying to debug
- 17:15and optimize scripting performance if
- 17:18you want to uh try and improve your
- 17:20scripts to make it have less of a CPU
- 17:23time so basically um what this would
- 17:25look like is if you were to just go into
- 17:26the game and hit run then what we should
- 17:29see is just an arbitrary number that
- 17:31shows how much CPU processing time is
- 17:34being used inside of Roblox Studio but
- 17:36we can actually use it in a certain way
- 17:38to help us understand how much
- 17:41processing is required to perform a
- 17:44certain complex task and so this is
- 17:46basically how I'm going to show it to
- 17:47you so let's actually comment this out
- 17:49and what I'm going to do is drop two
- 17:51lines and I'm going to have a start time
- 17:53so the start time is going to be local
- 17:55start time equals os. clock just like
- 17:58this and I'm also going to have an end
- 18:00time so I'm going to say local end time
- 18:03equals os. clock open and close
- 18:05parenthesis and then finally I'm going
- 18:06to have a print statement which is going
- 18:08to give us the difference like the time
- 18:10that that has elapsed between the end
- 18:13time and the start time so it's going to
- 18:15be end time minus start time just like
- 18:18this and that is basically all we have
- 18:20going so far but right now it's not
- 18:23really going to it's not going to be
- 18:24telling us much because the time that it
- 18:26takes to execute this line right here
- 18:28and the time that it takes to execute
- 18:30this line is going to be almost like
- 18:33nothing basically so if we hit run then
- 18:35what we should see in the output is a
- 18:37very very very very tiny number here
- 18:40with like you know and we can tell it's
- 18:42tiny because it's using an exponential
- 18:44value of-7 to the left side so that
- 18:46means it's a very tiny number um if we
- 18:49had a complicated calculation in between
- 18:51the start and end time then it would
- 18:53actually give us a more precise number
- 18:56to show how much CPU time it took to
- 18:58execute what whatever was inside of here
- 19:00so let's make something very complicated
- 19:02let's use a for Loop let's say 4 I = 1
- 19:06comma and then let's put in a really
- 19:07large number like I'm going to put like
- 19:10this really massive number right here
- 19:11and I'm going to say do just like this
- 19:13and what I'm also going to do is let's
- 19:16make an accumulator value like a sum
- 19:18value basically so I'm going to say
- 19:20local sum equals z and I'm going to set
- 19:24sum uh equal to whatever the sum is plus
- 19:28I now if you want to shorten this we can
- 19:30actually just say plus equals I just
- 19:32like this now obviously this is going to
- 19:34take a lot of processing power to do so
- 19:35I'm actually going to keep track of how
- 19:37big this number actually is by adding
- 19:38underscores and as you can see we're at
- 19:40100 million if we were to go into the
- 19:42game and hit run then what we should see
- 19:45is that it took 0.5 seconds to process
- 19:48um whatever was inside of this code
- 19:50right here but if we were to make this
- 19:52an even bigger number so if we were to
- 19:54say let's say 1core 0 so instead of 100
- 19:58million we are now reaching 1 billion so
- 20:01if we were to run this code then it's
- 20:03going to take a little bit of time it's
- 20:05going to be very slow for the CPU to
- 20:07calculate everything that's going to be
- 20:09inside of the script and as you can see
- 20:11this took 5 Seconds to calculate what
- 20:13was happening inside of the script
- 20:14rather than 0.5 seconds so I would
- 20:16recommend using os. clock if you want to
- 20:19optimize performance within your scripts
- 20:21so that you know how much time it takes
- 20:23to do certain calculations so that's
- 20:26another useful time function that isn't
- 20:29part of the daytime library that I hope
- 20:31you'll find to be useful inside your
- 20:33scripts okay so now I'm going to show
- 20:35you some time functions that Roblox has
- 20:37for us that I recommend not using and
- 20:41the reason I recommend not using these
- 20:43is because um these functions are
- 20:44outdated and uh and these are replaced
- 20:47with better functions with the ones that
- 20:50I've showed you to use inside of this
- 20:51video but just for educational purposes
- 20:53I'm going to show you what each of them
- 20:54are so that you know not to use them so
- 20:57I'm going to go back to my script and
- 20:59I'm going to uh comment out everything
- 21:01that's inside of here and the first one
- 21:03that I'm going to show you is Tick open
- 21:05and close parentheses now basically what
- 21:07tick does is it Returns the number of
- 21:09seconds that have passed from the
- 21:11current time uh and also the Unix EPO
- 21:14time now from what I've showed you with
- 21:17datetime it basically does the exact
- 21:19same thing with daytime. now I would say
- 21:22for newer work you should use daytime.
- 21:24now rather than using tick because it's
- 21:26a much better feature to use than using
- 21:27tick it's pretty outdated for
- 21:29localization reasons and also because it
- 21:32doesn't work compatibly with all time
- 21:34zones so that's like a general
- 21:37explanation that I can give as to not
- 21:39using tick so the next one I recommend
- 21:42not using is os. time now this is part
- 21:45of the OS library and basically what os.
- 21:48time does is it Returns the number of
- 21:50seconds that have passed since the Unix
- 21:53Epoch time kind of like tick it's a
- 21:56little bit better than tick but it's now
- 21:58been superseded with daytime. now like I
- 22:01mentioned earlier before so I would also
- 22:04say you should not use os. time in newer
- 22:06work but if you decide to use it those
- 22:08are the similarities os. time is similar
- 22:11with datetime.now but I would still say
- 22:13you should use daytime. now rather than
- 22:15using os. time and the next function is
- 22:18os. dat which is also part of the OS
- 22:21Library so os. creates a date for us to
- 22:26use but the the difference here is is
- 22:28that it's not wrapped in a datetime
- 22:30object but instead it's a simple
- 22:33dictionary full of key and value pairs
- 22:36that we can use to have a date now again
- 22:39we should not use this because date time
- 22:41provides localization which is very
- 22:43important when it comes to creating date
- 22:45times and also stamps or creating time
- 22:48stamps within Roblox studio so this is
- 22:50also not recommended and finally os.
- 22:54time now to be honest this one is kind
- 22:57of interesting because it can either go
- 22:59both ways of whether you think this will
- 23:01actually be useful or not but in my
- 23:02opinion I would say you don't really
- 23:04need to use this and the reason I say
- 23:06this is because basically what diff time
- 23:08does is it takes two date timestamps and
- 23:12it finds the difference between those
- 23:14two so basically it's the same thing as
- 23:16finding the elapse time between two
- 23:18timestamps but you can simply just do
- 23:20this by subtracting the Unix timestamp
- 23:23from one date and the Unix timestamp
- 23:24from another date and you get the
- 23:26difference of time in there so I don't
- 23:27really see what the point of having this
- 23:29function is I could be proven wrong but
- 23:31from what I've been able to experiment I
- 23:34don't think there's really any reason to
- 23:35use this and also because I don't think
- 23:38it's compatible with daytime objects as
- 23:40well so I would give a recommendation to
- 23:42not use diff time as well from the OS
- 23:44Library so with these four functions you
- 23:47should not use them in your newer Roblox
- 23:49games but instead use the functions that
- 23:52I've provided to you from the other
- 23:53parts of this tutorial so date times and
- 23:57also OS clock and normal
- 24:01time so for today's challenge I want you
- 24:04to recreate this um expiration Code
- 24:07system right here where basically what's
- 24:08going to happen is when we insert a code
- 24:11that either passes an expiration date or
- 24:14has not passed an expiration date that's
- 24:16going to determine if this code is
- 24:18redeemable so when we select this text
- 24:21box right here um we can basically put
- 24:23in whatever text we want so the valid
- 24:25code is just simply called valid code
- 24:29just like this and when we hit our Enter
- 24:31key then it's going to say on the bottom
- 24:33of the screen whether the code has been
- 24:35redeemed or if the code is expired or is
- 24:39um invalid so I'm going to show you what
- 24:40this looks like on the opposite end
- 24:43[Music]
- 24:43[Applause]
- 24:47[Music]
- 24:50[Applause]
- 24:52now all right so I changed the
- 24:53expiration date so now if we select so
- 24:57now if we type in Val code once again
- 24:59then it's going to say that the code is
- 25:00expired or invalid because it passed the
- 25:03expiration date so that's what I want
- 25:05you to do with this challenge I want you
- 25:07to use timestamps to be able to give an
- 25:08expiration date and check if the current
- 25:11time right now has either passed the
- 25:13expiration date or has not pass the
- 25:14expiration date and I know a tool that
- 25:16will help you with this challenge I'm
- 25:18going to show you this right
- 25:19now okay so I'm on this website called
- 25:22UC time.net and basically what this does
- 25:25is it shows me what the current UC time
- 25:27of of the day is right now because I
- 25:30think if we're going to be uh creating
- 25:32an expiration system I think we should
- 25:34be using UTC time that's more General
- 25:36across the entire world versus our local
- 25:39time so this is my local time right here
- 25:41but what we're interested in is this
- 25:43time right here so I want you to use
- 25:45this time like whatever it shows right
- 25:47here to determine if it's an expired
- 25:49code or if it's a redeemable code
- 25:51currently right now so you can use this
- 25:54if if this will help you but this is
- 25:55something that I'll leave for you in the
- 25:56description if you want to use it so
- 25:57with that being said I'm going to let
- 25:59you off to try this Challenge on your
- 26:01own uh come back once you're done so
- 26:02that we can go through this together all
- 26:04right so what I'm going to do is go on
- 26:06the right side of starter guy and I'm
- 26:08going to insert a screen guy and then
- 26:10inside of here I'm going to create a
- 26:13text box like this I'm going to take the
- 26:15text box I'm going to scale it up just a
- 26:17little bit and then move it towards the
- 26:19center kind of like right here oh I
- 26:21don't want to type anything in there um
- 26:23and so now what I'm going to do is
- 26:25scroll down here and then create a
- 26:27placeholder text which I think I
- 26:29remember it was like uh type code here
- 26:32dot dot dot something like that and I'm
- 26:34also going to make the text scale just
- 26:37like this so now we have our text box
- 26:39and now we're going to make this
- 26:40functional so we're going to uh hit the
- 26:41plus sign and we're going to insert a
- 26:43local script inside of the text box
- 26:45going to delete this and first thing I'm
- 26:46going to do is make reference to the
- 26:47text box by saying local textbox equal
- 26:50script. parent and then I'm going to say
- 26:54uh two pieces of information that we are
- 26:56going to need the first one is going
- 26:58going to be what the code actually is
- 27:00and the second one is going to be the
- 27:02expiration date timestamp that we're
- 27:04going to create to let us know what the
- 27:06expiration date actually is so the first
- 27:07thing I'm going to do is create a code
- 27:10uh string by saying local valid code
- 27:13equals in quotations we're just going to
- 27:15say valid code as the code itself and
- 27:18then we're going to create an expiration
- 27:19date we're going to say local expiration
- 27:22date equals and we're going to use
- 27:24datetime objects by saying datetime
- 27:28from Universal time because we're going
- 27:31to be using a universal time not a local
- 27:33time and now we're going to uh fill out
- 27:35our arguments by saying 2024 the month
- 27:38being June the day being 7 and the hour
- 27:42being 21 and then minutes and seconds we
- 27:44can just make that zero now again I
- 27:46think you should change this to
- 27:48something that's more relevant to what
- 27:50your time currently is or at least a
- 27:52time that's closer to where your time is
- 27:55right now versus what I have over here
- 27:57okay so now what we're going to do is
- 27:59use an event to tell uh whether the user
- 28:03has pressed enter on their keyboard um
- 28:06after they lose focus of this text box
- 28:08so there's actually an event for text
- 28:10box simply just called Focus loss that
- 28:12we're going to use and if you want a
- 28:14more in-depth guide on using uh
- 28:16textboxes then I'm going to leave a link
- 28:18for that for you right here to watch
- 28:19essentially what we're going to do is
- 28:21say text box. Focus lost colon connect
- 28:25function open close parentheses enter
- 28:27and and there's going to be one argument
- 28:29here that we're actually going to use
- 28:31and it's a Boolean that basically says
- 28:33if the player has pressed the enter key
- 28:35as it's being selected so we're just
- 28:37going to say enter pressed like this and
- 28:39we're going to make a simple check to
- 28:41see if the player has uh pressed their
- 28:43Enter key then we're going to make two
- 28:46checks so the first check is going to be
- 28:49whether the current date has passed or
- 28:52has not passed the expiration date and
- 28:54we're also going to check if the code
- 28:56that the user entered is valid and it
- 28:58matches the code that's up here so what
- 29:00we're going to do is say if and we need
- 29:03to calculate how we're going to
- 29:05determine if this date has been expired
- 29:08or not so in parenthesis we're going to
- 29:11first put in the expiration date and as
- 29:13you know the expiration date is the
- 29:15number of seconds that have passed since
- 29:17the Unix Epoch and if we basically take
- 29:20this date and we subtract that with the
- 29:22current date so we basically have a
- 29:26current date variable probably somewhere
- 29:27over here so before this I'm actually
- 29:29going to say local current date equals
- 29:33date. now open close parentheses or
- 29:35sorry uh I meant to say date time and so
- 29:40now what we're going to do is say
- 29:41current date if this is greater than
- 29:45zero so basically if we have the
- 29:47expiration date so this date that's like
- 29:49right over here if the time uh elapsed
- 29:52since Epoch if we subtract that with the
- 29:55current date and if the number number
- 29:58that we get is going to be greater than
- 29:59zero meaning if there is time that is
- 30:04meaning that if there is a discrepancy
- 30:05in time between the expiration date and
- 30:07the current date that means the the time
- 30:10has already passed I hope this
- 30:11calculation has made a little bit of
- 30:12sense to you in how we understand where
- 30:14this actually came from and how this is
- 30:16relevant to uh what we're trying to
- 30:18calculate up here so this determines if
- 30:21the the date is past the expiration and
- 30:23now what we're going to do is also check
- 30:25if the code is valid so we're going to
- 30:26say string lower and the reason we're
- 30:29using this lower method inside of
- 30:31strings because we want to take whatever
- 30:33uh text is inside of this text box and
- 30:35see if it matches this one perfectly
- 30:38like exactly this string right here so
- 30:41inside of here we're just going to say
- 30:43uh text box. text like this and then
- 30:47we're going to make a print statement
- 30:48saying code has been uh redeemed
- 30:52successfully like this and uh if it
- 30:55fails then we're just going to print
- 30:57saying um code uh is
- 31:02expired or invalid and there we go we
- 31:06basically have our script right here
- 31:07that determines um if the code is
- 31:10expired and if the code is actually
- 31:11valid so now if we go into the game hit
- 31:13test and hit play and see this result in
- 31:15game then what we should see is if we
- 31:17type in valid code like this oh sorry I
- 31:21forgot to do one thing so we actually
- 31:22have to go back and specify that we're
- 31:24not trying to operate on datetime
- 31:27objects but we want to get the Unix
- 31:29timestamp as a number for the expiration
- 31:32date and the current date so my
- 31:35apologies on that now if we go into the
- 31:36game and see this for ourselves then uh
- 31:39what we should see is if we enter the
- 31:40code right here by saying valid code
- 31:42like this then it's going to say the
- 31:44code is expired or invalid but if we go
- 31:46into the game and we change this hour
- 31:49from 21 to 22 because right now the
- 31:52current hour for me as of recording this
- 31:54video is 21 if I change this to 22 then
- 31:57it's going to say that the code has been
- 31:58redeemed successfully and of course I
- 32:00can also add something invalid to this
- 32:02um and I I guess it's still going to say
- 32:04the code has been redeemed successfully
- 32:06um weird okay I guess like that part
- 32:08doesn't really matter too much what
- 32:09matters more is we were able to uh
- 32:12calculate successfully whether this uh
- 32:14whether this code has been expired or if
- 32:17it has not been expired using the
- 32:19daytime objects so I hope um this has
- 32:21given you insight into adding this
- 32:23inside of Roblox Studio to keep track of
- 32:25time and also all these other things
- 32:27that you can add inside of studio with
- 32:30daytime and all these other time
- 32:31functions that I explained to you in
- 32:33this episode so if you found this to be
- 32:34helpful be sure to leave a like on this
- 32:36video and if you'd like to see an
- 32:37in-depth tutorial on textboxes then I
- 32:40will have a link to that for you right
- 32:42here to watch so go ahead and do that if
- 32:44you haven't already and I will see you
- 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.