0:00 hey thanks for stopping by in this episode of my Roblox Advanced scripting tutorial series I'm going to be going over roblox's task scheduler and it's the functions that they've provided to us to be able to use inside of our games not only that but I will be showing how to use these functions and when to actually use these functions inside of your game so with that being said to understand what I'm talking about let's jump straight into an example all right let's think about this for a minute so over here I basically made some parts that represents tasks and alongside it I have a bunch of other parts that represents the functions that we're going to be using that's provided to us with the task scheduler so I want you to look at these tasks and imagine them being in inside of a script so we have the very first task that's going to be the first thing that executes whenever
0:47 we write a script and then we have this be the first thing that runs so let's say inside of a script we have task one and then we have task two that executes right after task one is over these two tasks will run run one after another once the first task is done and the second one is done but what if we want to add more control to when these tasks actually do run one of the ways to do it is use a weight statement here um in which we basically just like put this right in between task one and task two where we call in the weight function and give it like a random number of seconds to wait where we're literally telling the script to stop what it's doing before running task 2 or anything else that's after the weight statement there's also other things like delay uh and this would primarily be used if we want something to run inside of the script after
1:35 a specific amount of time and uh we can continue running stuff in the meantime while we wait for the task to actually begin so if I bring in task three down here then what's going to happen is Task one is going to run first and then task three and then uh as soon as the delay is finished with task two that's when it's going to run so it's going to go from one to three and then to two and uh let's say we want to actually cancel this delay uh we would just take the cancel function and we would just kind of you know there's also other things like if we want to run two tasks at the same time we would use the spawn function and also defer but that's a little more complicated to explain so I'll just explain that roughly through scripting but uh I kind of hope that this little
2:21 analogy helped bring awareness to what we're going to be doing inside of our scripts at least I I hope so um but with that being said let's start scripting this stuff now that we understand it a little bit let's start scripting so on the right side with the Explorer let's click on the plus sign for Server script service and we're going to insert a script uh let's delete this and um let's represent our tasks from the example that I showed you so in the comments we'll do a double dash and we'll say task one and then we'll drop two lines and then we'll make another comment by saying task two so let's make these two tasks actually do something something so for task one let's make a print statement by saying print and then in parenthesis uh we're going to let's say
3:07 print before and then for task two let's print after and so now we have these two tasks that we want to execute but in order to control the flow of when we actually want to execute these tasks we'll be using some functions given to us by the task scheduler uh to make that happen and the first thing I will introduce to you is task. weight so so let's just drop two lines here and then in the center here in line four um let's type in task. weight and then inside of these parentheses uh this takes in a number value which indicates how many seconds we want the script to basically freeze until we continue execution with whatever is going on down here so let's put in 3
3:53 seconds of execution time um and let's see what happens with the script so let's go back back into the game let's hit test and uh we're actually not going to hit play we're we're just going to click the drop down and hit run because we don't need our character to be in the game for us to actually test this so let's hit run and see what happens in the output so before is immediately printed and then after about 3 seconds that's when after printed inside of the output uh as you can see down here so yeah uh very self-explanatory um task. weight it basically just tells the script to stop what it's doing and wait for however many seconds we put into here before continuing execution with uh the rest of the script so the next function I'll introduce is task. delay and
4:39 pretty much how this works is that we want to execute a task but we don't want the script to actually stop what it's doing before executing what we needed to execute this can be something that can happen in the background after we wait a certain amount of time and then execute it to show you what I mean um inside of our task ask. weight let's actually comment this out so that you know the script is not affected by it cuz we don't need to use it anymore um let's drop a line here and let's put in comments task three so this is going to be the third task that we're going to run using our delay function so let's drop another line and uh what we'll say here is task. delay and
5:24 the way the syntax here works is that we're this actually takes in two arguments so the first one is however many seconds we want this task to delay before actually executing so we're going to put in a time of let's say 5 Seconds uh and then we'll put a comma here and we have to call in a function um that will you know be our next task that we're going to create you can either create a new function with this argument or you can pass in a function that you already created beforehand but let's just create a new function so let's say function open parentheses close parentheses enter and now let's make another print statement so we'll say print and then inside of here we will say after times two essentially what we're telling the script to do is to run the first task which
6:12 is before and we will tell the script to delay execution of this line for 5 seconds first uh and in the meantime it will run everything else that's inside of the script so it will run task two and then after 5 seconds is over it'll run the third task so let's go into the game hit run and we should see this inside of the output so before and after was printed at the exact same time and then after 5 Seconds that's when aftertimes 2 was executed because like I mentioned task. delay does not pause the script execution like waight does we only use delay if we want to run something after a specific amount of time without affecting anything else that comes after it okay now let's
7:00 say we have a delay function here but but what if we don't want to execute what's currently inside of this delay anymore um in case like something happens in case like something in the game or inside of the script has changed its conditions and uh we don't want this delay to uh affect the script anymore because we don't need it then we will use a function called cancel the first thing we're going to do before actually calling cancel is wrapping this entire delay inside of a variable so before task. delay let's create a new variable called local and we will call this delay task equals so now we've pretty much wrapped this entire task. delay block of code inside of our variable let's make a condition to where I'll put this up for Chance by picking a random number and
7:51 if that random number is the number that I pick it then we're going to cancel the task we're going to pick a random number from 1 to two so say local random number equals math.random and inside of here our minimum value will be one and our maximum value will be two and let's write an if statement by saying if random number we will see if random number equals one so there's a 50/50 chance of whether this is going to execute or not then inside of here we will say task. cancel and we will throw in our delay task variable and uh let's also write a print statement down here to let us know that the random number has been picked the random number has been picked so let's go and hit
8:41 run and see what the odds are so as you can see uh it looks like I got lucky so the random number has been picked meaning that uh we cancelled our delay task so it's been about 5 seconds and uh I don't see any aftertimes 2 printed down here that's because we effectively cancel the task that we no longer want to use anymore and so if we try this again and if we try this again then hopefully uh we should see the other results so yeah after times 2 has been printed because we still have a 50-50 chance of whether this task will actually run or not this is a very specific example um when you're using this in an actual Roblox game it's probably going to look a lot different but the idea is still pretty much the same the next function I'll show you is task. spawn it's
9:29 basically used if you want two tasks to execute at the exact same time of execution I finds the most use out of task. Spawn when I'm working with loops and I will show you why so let's actually delete this middle section here uh since we're not really going to be using this anymore and I'll also delete this task. weight statement we don't really need that anymore either and I will rename this to task 2 and task three so we'll create two for Loops that will just print out random stuff so let's start with the first Loop by saying 4 I = 1 comma 10 do so we're basically going to run a task from 1 to 10 so it's going to so whatever we put down here is going to execute 10 times and inside of here we will write a print statement by saying print for Loop one and then down here let's
10:20 write our NE next task so we'll actually call this task three and after will be task 4 so down here we will write another for Loop by saying four I = 11 10 do and we will write print for Loop 2 so it looks like Loop one executed first like the entirety of loop one executed first before going on to execute Loop two I would say this is pretty useful if you just want to work with one Loop and then immediately go to the next one after that but what if we wanted to run these two for Loops simultaneously or just any Loops in in general task. spawn gives us the power to be able to do that so instead of having one for Loop let's make this into a while loop instead so let's delete
11:08 our first for Loop and we're going to create a variable that will be our condition for our while loop so we will say local is looping equals true and then down here we will write while is looping do and then we will print while loop task. wait you can actually not put a number inside of here um you could just say open parentheses and close parentheses and not put anything in there Roblox will just put in a very very small number for you uh if you didn't put in a number and uh we'll do the same thing for down here inside of our uh third task so we'll drop a line and then we'll say task. weight here as well uh and then after this for Loop let's set is looping to false that
12:01 way once this for Loop is done then we're going to set the is looping variable to false that way we know to actually stop this while loop at the same time now you might think okay well where is task. spawn I didn't write task. spawn yet because I want you to see what happens before I Implement that so let's go into the game and hit run and see what happens so as we can see while loop is constantly being printed every like millisecond or some really small number it's continuously inside of an indefinite wall Loop because this Loop is going to continuously run as long as is looping is equal to true but we said down here that after we printed this Loop 10 times we're going to set
12:47 is looping to false but the issue is this Loop is always going to run as long as is looping is set to true and because that's the case we're never going to come down down here to run task 3 and make is looping false that's why we need to run these two Loops simultaneously so that we can get the result that we want so let's hit stop and now let's finally wrap our while loop inside of a task. spawn function so after our is looping let's drop two lines and we'll say task. spawn and inside of these parentheses they it takes in one argument which is a function now again we're just going to create this function directly by saying function and then open parentheses close
13:33 parenthesis hit enter we will just take our code that that was over here on the outside we'll right click cut select this right click paste so now that this is wrapped inside of a task. spawn function supposedly this is going to run as well as our for Loop so that way it can actually go from 1 to 10 and then it will stop the loop whenever we need to so let's go into the game and then see our new change as you can see the while loop successfully kept uh printing the while loop print statement and it also uh kept printing the for Loop 10 times and then as soon as the for Loop was done that's when is looping was set to false and that's what caused the while loop to stop because now is looping was set to false so that's the power of using task. spawn
14:23 to be able to primarily work with multiple Loops at the same time at least that's generally what I use task. spawn for and so this is just something very powerful that I highly encourage you to use inside of your scripts and the last function I'll be teaching in this episode is task. defer and uh before I explain it let's actually write it down first so let's get rid of task three and we'll also get rid of task two all right and so now the way the syntax works is that we're going to say task. defer and the only argument here is a function and like like usual we're going to create a function inside of this argument so we'll say function open close parentheses enter and now let's print inside of here print defer that's what we're going to say Here and
15:12 Now for task three let's create a big for Loop that will print out like 10,000 lines of code so we're going to say 4 I equal 1 comma 10,000 do and we're going to say print for Loop one so now that we have our task. defer and our third task which will be a for Loop let's actually break down what task. defer actually does so we've gone over task. spawn which simultaneously runs two tasks we've gone over weight that tells the script to literally stop before executing the rest of the the code we even told it to delay um execution with a specific function so it can do whatever it needs to do before it actually executes the delay function but now now we're going to look
15:59 into defer which basically says that you can run this block of code once everything else has been executed and so it's not going to interfere with data throttling or any other problems that might occur during the script defer basically tells the script that you can make this the last task that executes in the code um it's not a huge priority but we're still going to do the task regardless it's just it'll pick the best time to be able to print out defer without interfering with the script so let's see what happens if we go into the game and hit run so it's it's lagging a little bit because we're run we're printing 10,000 different things so as you can see it ran before and then it immediately it skipped this part it skipped task two and it went into task three right away by
16:50 printing out 10,000 lines and then it printed out after so once everything was executed it then went into defer and did what it needed to do inside of here so I guess you could say this is very similar to delay where we gave it a specific number of seconds before executing what was inside of here but this time it's kind of like a combination of delay and spawn where like it will execute right away at the most convenient time usually after everything else has been executed then it will print this to summarize what we just learned Roblox provided us a Tas scheduling Library filled with functions that we can use to control when we execute tasks inside of our script the first
17:36 function being task. weight which is probably the most commonly used one the most commonly used function which completely pauses the script before continuing uh any other task that comes after the second one is task. delay which delays a specific task after a certain amount of time has been given and then there's task. cancel which cancels a delay in case condition change where we don't need the delay anymore the fourth function is task. spawn and this is mostly used with loops to be able to simultaneously run multiple Loops at the same time and then the last function which is the least commonly used one called task. deer which basically prioritizes what tasks matter more than others during execution so an example of this would be getting a table of all the players inside
18:24 of the game and updating their player stats to be able to efficiently update them in the most optimal way now there are other Concepts to handle multi-threading like core routines but we're not going to go over that in this episode because core routines are a little more complicated but you do have more power in control flow and threading with your scripts so in the future I will make an episode on that but this episode serves as an intro to threading by using roblox's task library and so that's where I'm going to leave it off here be sure to join my Discord the link is in the description but apart from that I hope to see you in the next episode take care