Advanced Roblox Scripting Tutorial #32 - Spawn() & Delay() (Beginner to Pro 2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys welcome back to a brand new robust strip tutorial and in today's video I'm gonna be explaining the spawn function and the delay function and you may have seen this before and I may look a little confusing and they're actually pretty simple honestly but I'm gonna explain these in this tutorial and yeah alright so let's go ahead and get started so first things first you want to go ahead and make a script okay and then after you get your script obviously write some code but before we get to this I just want to say this may be a little bit confusing so if it does it confusing don't worry I swear you'll you'll learn just have to stick with it and just maybe watch this video again if you do get confused but it's actually pretty simple so because some people have trouble with it anyways alright so here we go alright so I'm gonna - I'm gonna teach you guys first a spawn function and what it does okay so okay for example let's say I have this a while loop okay let's say okay sorry I put a script into my server subscribe let's go ahead and do that already if you guys haven't but anyways when we do that again but let's say I have a while loop okay and all I want this trip to do is just prints okay I'm gonna do wild true do and I just wanted to print hey alright that's it that's it I just wanted to keep printing hey okay and if I hit run it'll work just fine it'll do that it'll keep printing hey as you can see here in a second it'll just keep printing hey and it'll keep printing and printing and printing and it won't alway two minute never mind I just broke this script I forgot to put a wait that's my bad make sure you put a wait in there cuz it said hey the time 16,000 so make sure you put a weight in there it's my bad anyways but it will work fine to put a wait in there okay so as you can see here now if I run it and there we go hey and then it says times 50 times 60 times 90 times 100 and it's just it's just printing hey a lot okay I'm not sure to the number but it is so yeah anyways but what I wanted to do now is for example what if I want another loop in there and I wanted that loop to prints prints cheese okay if I wanted to print cheese watch what happens okay it doesn't print cheese it's not printing cheese it's not working at all it's not it's not printing right now obviously it makes sense though because currently it's in this it's in this loop it's it's stuck in this loop right now because it runs you know that's how cool it runs from top to bottom that's how Lua works at least butter on top to bottom so it hasn't got to this yet and the thing is it'll actually never get to this because this is a wild true do loop right and in a while true do loop it's true so it'll never end its while it's true and it's always gonna be true so basically this is just an infinite loop and it'll never end but the point is we're not printing cheese and we wanted to print cheese and we wanted to do at the same time now obviously we could just put cheese in here but because you know for better ok like you wouldn't actually use this for this example probably but like there's actually real like real real situations you would want to actually have 2 loops for and same time like for example what if you wanted um like like ok when you have like data stores sometimes you wanted it in your data store you wanted to like save every like 10 seconds so you do like while true do and then be like oh wait 15 seconds and then you know save or whatever Bob's hisilani do it but you'd want it to run at the same time with the rest your script is running right and there's actually a way you can do that and now I'm gonna teach you that in today's video that's what spawn is ok spawn right here as you can see if I type in spawn what's gonna happen is it'll actually spawn a function for me and what spawn means is it's gonna create a new thread okay so if you ever heard the word thread before it's okay I basically this is like multi-threading right so well what you can do is you can have this loop running right here okay and then at the same time you can spawn in another thread which you just make a function right you only just make a function or you could type like function right here and then write your code here and then you could you know like print hey and now work as well but normally you just actually make a function called local function prints hey that's what they'll get so you want the function to be named right and then you write your code in here like hey and then erase you know sorry cheese cheese and one of the print cheese print cheese and then you know down here you'd have it and then you just pass the function name inside of here and then you just spawn print cheese and what's gonna happen is it's actually this isn't even a while loop my best video while true do and then in there there we go and now this is just a wild shrew do loop inside our function called print cheese and it's gonna she's gonna print cheese infinitely but what's gonna happen now is it's gonna run this loop okay and then it's going to spawn and cheese but you might notice written she's not gonna work let me tell you that one second so if I run this right now it doesn't work okay it's not actually it's not printing cheese at all I'm not sure you see that but it's not printing cheese at all okay so what we need to do is let me zoom in a little bit her two guys say I'll zoomed out a lot sorry about that anyways okay so what we need to do actually is spawn it before we start a little because right now we start we start the loop up here so it never gets to here so you need to actually make sure you spawn first before you actually go into any sort of while loop and this doesn't necessarily mean you have to have to while it's running at lunch but just because maybe if you want like two threads when anyone's like one while loop and then you also want to be able to run another function that events in the same script this is how you do it and actually I messed up again I forgot to put a weight in there my bad guys make sure you put a weight inside of your wild sure do loop because what happens is it runs so fast that it just crashes your game so it's gonna go ahead and break the script here in a second and then thankfully cuz row boxes or block cities like this it'll um fix it so wait a second here I'm sorry I'm playing with my ear pods this is not a Moran deal okay okay it's scratching now that's my fault it's my fault but hopefully it'll fix you in a second or I'll open up a new roblox studio that doesn't work alright anyways so yeah my my thing crashed my bad beautifully know what that happened alright so let make a new base plate and close out of this one for now I'm just BAM clip and then hit close program alright anyways so let me go and write that code again one more time but yeah that's what that's what it does okay you spawn in a function so local function print cheese and then you know while true do and then Waits don't forget the wait and then print cheese and then you spawn it in right here okay and then obviously it's gonna keep running and now let me actually do it for you guys so I'm not pranking you you know I'm not like you line I have some proof okay and then I'll print hey down here okay anyway so as you can see it's going to spawn in the function and then it will also be printing hey we cuz I have a while loop here says hey and then I have a while difference that's cheese and since we spawned it in we created a new thread for our trip to run and now it's going to run now keep in mind whenever you're doing this make sure that okay as you can see it's working it's working good actually hey cheese hey cheese 80s a cheese whenever you're actually doing this guys make sure you keep in mind that do not spawn too many threads it's gonna break your game keep keep the thread spawning to a minimum okay because it's just gonna cause problems so key threats writing to a minimum I guess that okay anyways that's that's that alright so and now that's pretty much spawn you're just wanting a new thread so which is pretty cool but we don't need that anymore but there is times you'd use that guys trust me you'll figure it out one day but uh there yeah anyways alright next function is delay which is pretty simple or pretty similar but I don't know if it's infinitely spawning when you thread it but it's just creating a new thread for a temporary time I guess but it allows multiple Lua threads to be executed at the same time which is good so how you do this okay is what you're gonna do is you're gonna go ahead and type it here okay actually we'll take that little while loop back right here and then what we're gonna do is type of here delay actually let me I'm just gonna keep all this besides besides the spawn delay is kind of similar actually but what delay does is okay for example let's say for example you wanted your script to keep running right or like a little you were doing something in the script like you were doing this and then you wanted to do this okay but you wanted to make your script wait a few seconds before it did this thing right here right before it did that this little function whatever this is you wanted to wait like five seconds but at the same time you also want to do a while true do loop down here to keep running now obviously you could you spawn but delay what delay does is it actually creates a new function or schedules a function and it tells it to be executed after a certain amount of time has happened so basically as you can see here what I'm gonna do is I'm going to go ahead and type delay okay and then I'm gonna type the number of time in seconds so I'll do like five seconds and then I'm gonna type the function that I want it to run after five seconds and that's gonna be cheese okay now if I were to use a weight right here in type of weight five and then put another function down here and then wait five and then have it run right well what happened is it would wait five but then this wouldn't be running yet it would have to wait till five seconds till this thread is until this thread is on pause and then you'd have to do this and then it'd have to you know do this now I'd obviously it's a function so it wouldn't run on its own but you know what I'm saying right you'd have to actually make it you it would it would stop everything else so let's for example let's say we just wanted it to not wait five and have it also run this but yeah okay so basically you'll see this that happen it's gonna rubble at the same time it's not gonna pile its current thread so it says hey right right now it's printing a even though it's down there and then we since we delayed five seconds it paused it or created a new thread and then it kept the current thread running but now it has a new thread running which is cheese okay so basically delays like spawn but with a weight in it okay and so I guess that's probably what it is and then also guys if you want it to you can sure you write the code in here and you can you can write the code in here and you can do that and then you could write all your code in here and this would be the same thing so if I just put that in there of the same thing because well it's like it's like when you're make an event you know how you do like connects and then function and then you write your code in here it's basically like the same thing right you're just putting the function in there and then writing the function in there as well so yeah but as you can see it'll give us the same effects let me run the script one more time I'm spawning in this time actually okay and here you go and wait five seconds and there you go it's printing all right so check guys hope you another video if you like always make sure to subscribe and leave a like if you did enjoy that that's been it though that is spawning subscribe for new leave like if you enjoyed and I'll see you guys the next one in the next strip from tutorial all right peace
Info
Channel: TheDevKing
Views: 30,170
Rating: undefined out of 5
Keywords: TheDevKing, Roblox Scripting Tutorial, roblox, studio, coding, tutorial, roblox studio, roblox studio 2020, spawn() roblox, spawn roblox studio, delay roblox, delay roblox studio, delay function roblox studio, roblox delay function, roblox spawn function, roblox spawn function with arguments, how to script on roblox, how to code on roblox, how to program on roblox, roblox built in functions, spawn function roblox tutorial, delay function arguments, advanced scripting tutorial
Id: lohokvnLYEU
Channel Id: undefined
Length: 10min 50sec (650 seconds)
Published: Tue Feb 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.