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