Advanced Roblox Scripting Tutorial #33 - Coroutine (Beginner to Pro 2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I reckon looking back to a brand new video it is your favorite youtuber year anyways in today's video I'm teaching you guys about coroutines and basically how they work how to use them what they are all that good stuff in today's video all right so let's go ahead and start so first things first I'm gonna my cereal went off learn about that alright the first things first go ahead and insert a script into your server ship service and then we go ahead and get started alright so what is a quarantine you may be wondering what exactly is a quarantine what does it do how does it work and that's all explained alright so first of all what is the quarantine so Cortines are pretty cool they can create new threads in a script basically what that means you guys don't understand what threads are in my last video actually explained spawning and delaying which are actually examples of core genes those func they're like built-in functions but I want to teach you how to make your own Cortines without using roblox's built-in functions anyways if you didn't watch that it's fine I'll explain it from the basics okay so wouldn't you make a new coroutine it's like creating a new script in your place right because like if you have a script right here okay and I print up start with that and I print cheese okay and then down here I print chicken okay chicken chicken and cheese I don't know why I picked those words but I did okay one is gonna print out for the other obviously cheese is gonna print first and then checking up for a second and it's not gonna have an exact same time because it'll print cheese and then a no prep chicken now let's say we wanted to print at the exact same time how we do that is we go ahead and put chicken in here alright and then it'll put the exact same time but we don't want to we don't even want to use two scripts that's too much work right I don't want to have to use two scripts such as annoying I want to keep on myself in one script so how do you do that will you create a new thread in your script and that's what cortines are it's creating new threads in your scripts so let me believe that old script print there and I'll make it from scratch alright so yeah that's what it is anyways you can actually save time some time because you you might actually have all the variables already in the script and that way enough to copy and paste and all that stuff and also cuz the execution time as well because you don't have to save all those variables again which is pretty cool okay anyways it basically allows you know thread somewhere not multiple times in the same script okay how we go and do this is you have to create a quarantine all right or not a Cortina quarantine there you go all right so you just do all you have to do to make a support team is you just do core team dot create now we could just do like this but we won't actually mess with it a little bit more so we're gonna do that she put inside a variable so we'll do local new thread is you go to 14 oh no not chicken core team create okay and then you just have function in here like a no like an event and then you just hit enter and there we go all right we have our everything we want to run my 14 basically this is like a new script okay this entire blank space right here is like a new script okay think of it like that and now we want to make it do something so let me get prints cheese okay and it'll make down here I don't like cheese and I'm one of the tired of cheese will write like cookies all right it'll not confuse all right I'll print cookies okay and then out here you want to print chicken actually no no no not milk ice cream all right there we go okay and now so we want to friend cookies up there and one for ice cream run here how do we do it okay well we have a core team so it should print at the exact same time right well first of all no it won't because obviously the Courtney's gonna come first but if you actually were having like a while loop you know I'm saying that would be running repeatedly then that would be useful because it would be able to run this and that at the same time now obviously not the exact same time but pretty close to it but don't worry about that anyways okay so yeah we're gonna go and put cookies in there and then it should work right it should print them both at the exact same time or theoretically the exact same time all right so let's go and hit run and let's see and no it didn't work a printed ice cream but it didn't print cookies now why is that it got past our core team but it didn't even print ice cream that's weird well it's not weird okay and the reason why is you have to actually actually start your core team to make it run before we can actually have it do stuff so how you do that's pretty simple all you have to do is type in core teen resume now you may be wondering why it's resume I don't even know it basically it's because you can actually pause coroutine to which I'll spend in a second as well but it should be like claw routine dot start right but no its resume I don't know why I presume because resume is like like restart not not restart but it's like start again right so wouldn't it be like courting zoom or something like that like you know I'm thinking too hard huh anyways Oh all right so codeine dot resume and then you just pass in the name of the thread which is there and then you just go ahead and hit run and then it'll work just fine I don't think it's here prick it prints cookies and it prints ice cream at the exact same time and now you could just put this on a wild lip right here you can do a wild sure do and then you could do wait and then you could put that in there as well and then even put a while loop in here as well so you can have two threads wanting out running at one time so I'll do weights and then I'll do it alright now as you can see what's gonna happen is we're gonna actually be able to run two threads at one time I think it's he it's moving super fast it looks super cool oh my gosh my eyes are actually like hurting a little bit what's going on I don't know they look like that for you guys too but like on my screen I see both words like my eyes at least are interpreting like this they see both words like the same time it's just as good as going so fast at the same time right now this is really cool because we have a new threat if I were to just put two while loops in my script it wouldn't work and obviously it wouldn't work because it's only one threat it would run from top to bottom and a while true do loop is never gonna end they never end it right it's Walter to do so it's never gonna finish the first loop so I think it teaches for anything that a lot is like times 60 times 80 times 90 up 100 but it's not actually even getting to the to this and that's because it's only one thread and it can do one thing at a time but with coroutines it can create another thread which is super cool alright anyways let's leave that for now alright I don't think you need to know about is QWERTY not rap and basically cortina rap is kind of like a replacement for quarantined out resume and create because basically what you're doing is you're you're putting think of it like a function with a cording jump inside of it okay so like you just do Cortino wrap right like this and then what happens is is when you call it you just do new thread like that and yeah now you're just wrapping a Cortina over a function which is it's kind of like reading it but it's not but yeah anyways then I can go ahead and print out here whatever I want like hey and then it'll call it as you can see and then there you go hey perfect alright anyways that's that's that okay also not parameters how you do parameters is pretty simple you just kind of pass them in here ABC and then you need to put them in here you know a ABC okay parameters we're done with those for now all right now once you're starting a quarantine R aesthetic you I said you can stop it all right well how do you stop it how do you stop your core team from running okay or how do you pause it because we can stop it or do just positive do whatever you want all right so how do you do that is you do coroutine taught yield okay actually person we explain to what record is actually useful for if you guys don't know like core teens are pretty cool because you can create threads in the script right but a practical example of what it would be for it's like datastore saving like an autosave at the bottom your scripts I'm Kylie make a data stories like to have like auto savings and bottom your script which makes it a lot easier to like read you can see everything in one script no if you like go to scripts that's pretty cool I use that a lot I usually I just use a spawn function to be honest but I probably should switch it to a quarantine because spawns aren't that great but yeah anyways alright so now we're gonna go ahead and do is yield it okay so how do you actually pause your quarantine you're good order your thread alright so what you do is first of all we'll go into our nest egg right here and we'll just Prince claw routine started running okay and then we're going to do korte n-- dot yield okay if you got somebody yield me to me it's like stop or like wait if you never drove before you probably wouldn't know what that means but be yet when you drive you know you're like yield side or just like wait but yeah anyways then we'll just put after the yield a little print prints hora teen is running again that way well no it's running again cuz right it'll yield and then we'll know it's running again so now we're calling okay we just call the court scene once and then it'll go through this it'll go through the script and then hits course you got yield it'll stop okay and then if it stops it'll never start again unless you start it again so you need to actually make sure you start it again or else it will never start again so now as you can see we have a new thread and then if we just hit run it only print court he started running it's never gonna get to that second part we never called it again but if we put it again as you can see here we can have co-routines running and the court seen is running again now it happened really fast because that's just how fast that's al-fath lua interprets code let me put a waiting here like wait three then as you can see right here it'll wait at three seconds and then it'll start running again so yeah alright so that's pretty pretty cool now one more quick function after explaining with 14 is coroutine dot status basically courting the status but it is is it just text the status of a core team and we're doing this make sure you actually make it just a regular 14 and then just pass through the name of it and then you'll see so if I go out and actually I'm not gonna yield anymore but if I go out and actually just check the status of this cortina as you can see here it's actually what's nice a is it's gonna say it's dead or it should say it's dead and it didn't even didn't even work well what happened oh what happened oh I'm stupid I didn't even print that out I just did claw routine dot status I didn't actually print out the status of it which is a string value and you'll get like three different values we get like either you'll get either running dead or suspended and you'll see weight shows me in a second but as you can see it's this suspended and now I don't know why it suspended actually if I don't it wanted to say that oh excuse me never started that my bad we never a card in the quarantine so it never ever works so if we actually start it and also we do 14 dot resume because currently it's like technically yielded so if we just root if we start it up or resume it and then it'll see there you go it says Courtney's not started running but then it says dead now why does it say dead that's kind of weird shouldn't be running well no it doesn't it's not it's not running the reason why is it because it actually knows it finished the script 14 the the heart is finished right there's nothing left to interpret nothing rough to code nothing left upfront and I office to do so basically means it's dead so dad means the coordinates are running and it's done it's done for now right like it's just done done for now at least but basically you also get more value which is running and now how you how we would probably achieve that one is if we actually had it doing something like in a wild it for example or if we had a like checking for events that's another way but as you can see here it should print out coroutine are actually it sees this obsessed suspended wait a minute why did I spend it hmm hmm it should say running I don't I don't know why it says that it says suspended it prints out really fast all right you know I'm gonna do I'm gonna actually wait a few seconds before it starts running maybe that's it because it's printed out first and wish it should better than it okay no it's a suspended well it it should be running I I don't understand that to be honest with you but alright very cool very cool it's not working all right if you get the point though it works kind of all right guys that's pretty much it that's pretty much how you check also you have another thing called core team doubt running I wish to the function of core team and basically it'll check to see what coordinates are running and it'll print out all the running quarantines also one more thing you have is is yield Abul and that basically means if you can actually yield your function there's a few exceptions for when you can actually yield your your Cortines and basically as long as it's not a meta table or it's not like a c function if you don't know what that is then you're probably not using it so don't worry but yeah all right guys let's run which i hope you enjoyed and I'll see you in the next one it's been your favorite youtuber ok leave a like if you enjoyed subscribe for new for more awesome tutorials just like this one and yeah that's it peace
Info
Channel: TheDevKing
Views: 26,483
Rating: undefined out of 5
Keywords: TheDevKing, Roblox Scripting Tutorial, roblox, studio, coding, tutorial, roblox studio, how to script on roblox, coroutines roblox, roblox coroutines, roblox coroutine, how to make a coroutine in roblox, roblox coroutine yield, roblox studio coroutine, coroutine in roblox, roblox coroutine.wrap, roblox coroutine.resume(coroutine.create(function(), advanced roblox scripting tutorial, roblox studio 2020, roblox studio tutorial, roblox scripting tutorial 2020
Id: f3bcZK7_F-E
Channel Id: undefined
Length: 12min 43sec (763 seconds)
Published: Thu Feb 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.