Wave Based Attacks - Tower Defense Tutorial #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my fellow gnomes and welcome back to episode two of our tower defense series now we're gonna need to add in some more zombies to our game because at the moment it's a bit rubbish just having the one zombie now obviously we could create a few copies like this we could duplicate them all and we could then run the game and oh yeah great now we've got four zombies but that's not really ideal because then we're gonna have all these different scripts that we're gonna need to take care of in each one really we need to do a bit of reorganization to our project and our code in specifically in order to get what we want so the first thing we're going to do well i'm going to delete those extra ones is i'm going to add in a script into server script service and i think we'll just name this one main it's going to be our main game script and we're also going to add a module script okay so inside of this we're going to add in something called a module script and we're going to name this one uh attacker or maybe we'll just call this mob for shorthand right and if you're not too familiar with module scripts then i've got a whole video all about that which i'd suggest you checking out we're going to continue here so we're going to name our module to mob and then obviously we need to return a mob and then we're going to create a function in the script do it spawn in a mob so rather than the zombie just kind of being stood there at the start of the game we want to add him into the game from our script now so we're going to add in our function function mob dot it's a module script you type the name of the module followed by the name of your function and the name is going to be spawn okay mob dot spawn and i think the parameter it will need to take is the type of mod so let's put the name and then maybe the map that we want to spawn them onto and so then inside of our main script we can then call this function so just to so we can check so working we'll print out name and map so we'll do here and then back in our main script we're going to want to access that module script so local mob equals require and we put script dot mob so then now inside our main script we can say mob dot spawn and we would then want to spawn what is zombie onto the the i don't know the grassland map right and then if we run that we should see down the output zombie grassland map because that's what we've instructed to do in our module script now at present we haven't actually got any map at all we've got this path so how about we sort of uh give it some grass so we can make it into our grassland so it looks a little bit more interesting and maybe we'll add some trees for decoration so now we've got something that looks a little bit like a map let's make sure we anchor all the trees we can group them together i suppose and then we ultimately want to put this whole thing inside a folder so we'll just call this grassland seeing as that's what we're going to call it and we're going to move everything into the grassland map the drooling zombie we don't actually want so we're going to move him over into server storage because we don't want him to be in the map to start with and then in server storage we'll add in a folder for where all our monsters are going to be so we can put mobs and then he can go in the mobs folder and let's rename him from drooling zombie just to be zombie so it's a bit easier to remember and now inside our main script we can say we get a variable for the map and we could say local map equals workspace dot grassland and then we can pass this in and then for our zombie we're gonna then want to check and see if there is something called zombie inside of that mobs folder we just created so in the mob module script let's have a look we'll say local mob exists equals and we're going to need a variable for the server storage actually always a good idea to put variables for cert for services at the top so we can say server storage equals game get service server storage and then we will know that the mob is in the mobs folder inside of server storage and then we're going to look for whatever they requested so find first child and the name that they've passed to that parameter so if the mob exists then we can start to move it into the workspace move into workspace that's just a comment but else if it doesn't exist then we'll just leave some kind of a warning shall we one uh requested mob does not exist and then maybe we'll just follow up with the name that they were trying to get but if they do exist then we're going to want to clone it into the workspace so local new mob this time equals that existing mob and then we're gonna clone it open close bracket once we've cloned it then we're going to want to give it a parenth and there's one more thing we need to do we need to set it a position so how about we add in a new part into the grassland map and we can just make this our starting point it doesn't really matter what uh hello material it is but uh i'll do this anyway right we'll make it force field why not okay something like this so you can kind of see that if it's red it'll be easy to see there we go okay so that's our starting point it's anchored disable on disable can collide and then if we've got a map as well we'll know that the the new mob got its humanoid root part and set the c frame of that equal to the map dot dot part we just created and get the c frame of that so remember c frame stands for coordinated frame it's essentially its position in the world and it's going to keep the rotation as well if i turn on the orientation indicator you'll see this part that f is facing forwards so anything we spawn in well with the same c frame as this we're in the same position and orientation as this part so now in our mob script we are seeing if they exist if it does then we're going to clone it we're going to set it to the position and then finally add it into the workspace so let's give that a try right we're trying to spawn in a zombie onto the map my main script let's just try running the game run the game and there we go a zombie has spawned straight into the spot now we will notice we've got an error down in the output that's from the existing script inside of the zombie if you remember before we created uh this script get him to move but we don't really want to have individual scripts for each zombie anymore so let's cut the contents out of here we can delete this script and instead we're going to do it inside of this mob module script now so we're going to create a new function at the top here function mob dot move this is going to want take his parameters the mob that we want and the map as well so now we can copy and paste that in and make a few changes obviously the zombie is no longer equal to script.parent but it is our mob parameter that being sent in so let's change this just to humanoid and we can make this equal to mob and we're going to wait for child now it's a good idea to wait for the humanoid to be fully loaded in wait for it to be loaded and the waypoints now these aren't just workspace.waypoints it's actually that grassland map so whatever map we've been given and then we will say dot waypoints so now we use that humanoid variable down here and then finally we'll head back into our main script and we are going to spawn in a couple more zombies as well so how about we do this inside some kind of loop say while or let's spawn in three of them so for i equals one three do and we just have that we'll wait one second or task.wait and then we will spawn in two more let's run that i'm going to see zombie spawning second one and a third one and they all spawn in at the same spot but we need to tell them to move don't we so back in our mob's mob module script and after we've spawned them in we can then call this move function so after we've done this we're going to say mob dot move and the parameters are the mob which in this case is this new mob that we've just cloned into the workspace and the second parameter is the map which we've already been given from here so we send that information as well and now if we run the script we can add the mob and it's going to move but we'll notice that it hasn't spawned in our other mobs why not we're still waiting here we told it to do it three times why isn't that happening well if we wait till this zombie gets to the end of the course and there he goes you'll see suddenly now the next zombie spawns in obviously we don't want this to happen we don't want the script to yield or be waiting for the entire zombie to get all the way around the course before the next one spawns in we need some way to have these different bits of code run at the same time we don't want our mob.move function to interrupt our spawning so how we're going to fix this is we're going to use something called co-routines now i'm not going to go into this in too much detail here but essentially i'm just going to put coroutine dot wrap and then i'm going to get these two brackets and inside here i put the name of my function which is mob dot move and then because i need to send the parameters as well i do another opening close bracket and then i can copy and paste those parameters in like so and if i then move that i can then spawn in my mob it will start to move it and then it will return and we can then spawn in another one so we're not going to have that delay while everything is being forced to wait but to move all the way around the map so now we can see we get those three zombies out out they come and they can all move together without the script sort of getting in each other's way a few a little more quick changes we'll do here the first one is we don't want them all piling up at the end of the course once they've got to the end we just want to remove them from the world and the second thing we probably want to tidy up this script a little so i'm going to stop the game and to get them to destroy once they've finished the course is pretty easy after we've looped through every single waypoint all we need to do is say mob destroy and there we go that's all we need to do because this line of code cannot run until it's gone all the way through the loop aka it's gone over every single waypoint now the next thing i want to do is try and improve uh this little bit of code here because this is all very well um but if i want to have a series of waves then i'm gonna have to have a lot of sort of things like this and it's going to be wrapped inside another big loop uh and it's going to start to look a little bit messy if i've got all these different loops going on so instead in our mob dot spawn function we could actually request a quantity to spawn in so after the name parameter we'll have a new one called quantity in the middle there make sure we've got a comma between each parameter and if we know that the mob exists then we can loop over the quantity so for i equals one we have the number of quantities the quantity is equal to one is just going to go through this leap the one time and we're gonna want to do all of this now we don't want them to all spawn in simultaneously so let's have let's try a half a second wait between each unit being spawned and now inside our main script instead of typing spawn lots of times you can just type spawn zombie three onto the map and so now we run this we should see one zombie second zombie third zombie they'll make their way around and there we go and they all get destroyed when they reach the end of the super so that's one wave of attack and if you want to add more waves all we need to do is write a little bit of basic logic here we'll type in another loop so we'll say i or wave equals one and then however many waves you want uh let's just do five waves to start off with do and we could create some logic here we could just say three times the wave is how many we want to spawn in so on the first wave that'll be equal to one and we just born in three and then six and then nine and so on or we could have a bit more fun here if we wanted so i could say if the wave is less than uh five then we'll do three times whatever wave we are but else if the wave is five so the last wave then in that case we're not just going to multiply it we could just straight up spawn 100 zombies all like that so they're gonna have a lot to deal with there and at the top we can print out wave starting and the name of the wave wave and then at the end we can print out wave ended and maybe we'll just have a short wait as well so task.wait one second and let's see if that's all working now click run again we're starting one we see the top and it's going to immediately start to spawn in the entire next wave of zombies we've got loads of zombies being churned out but we probably don't want them all coming out like this all together so we'll on the fourth wave we've got a lot of zombies already and now on the fifth and it's going to be churning out 100 zombies right now so we've got all manner of zombies being pumped out of the zombie factory but we want to give the defending players a little bit of a chance uh i don't think anyone's going to be surviving this are they so why is this loop just keeping going we're splitting them in we're waiting one second shouldn't it wait till all of these are spawned in well if you remember because we put it in this co routine we're actually not yielding at all so this function isn't going to wait until they finish moving it's just going to spawn them in and it's going to go straight back to the main loop so the way we want to fix this is we want to check that there's no more zombies on the map and so instead of just putting them into workspace like we're doing here how about we create a folder inside of the map i'm going to name this to be the mob folder and so we can parent them to map.mob and then inside of our main script after all of our spawning we can check so every second we will repeat until the map dot mob get children equals zero so the number of children so the hash map.mob folder and get the children and if it equals zero then we'll stop leaping but otherwise we will keep waiting so we can wait every second and every second we'll check is the folder empty and if it's not we'll keep going and now we run that spawn into three zombies and these zombies will keep moving around now until that mob folder inside the grassland map is empty so there's currently three of them in there and they're approaching the exit and they're being all deleted and then now look the waves ended and the wave 2 can now start and the next wave spawns in and instead of just waiting for them to go round i could just delete them all i delete this last guy and look wave ended and the third wave will then start so there we go we've got a sort of basic gameplay loop going on now we still need a lot more details to add here we need to get a base to uh take some damage here and we're gonna need eventually a way to damage the attackers but uh hopefully that's got you started with your wave-based attack system we're making a decent bit of progress i think here hopefully you've followed along if not then join me in the discord link is in the description and you can get some help there if you found this video informative then why not give it a like and even consider subscribing thank you very much and i'll see you in the next episode goodbye [Music] you
Info
Channel: GnomeCode
Views: 6,208
Rating: undefined out of 5
Keywords: roblox, tutorial, gnomecode, gnome, roblox development, lua, programming, scripting, programmer, how to script, learn to script, how to make a game, game dev, roblox game dev, roblox game development, roblox studio, tower defence, roblox tower defence, roblox tower defense, make a game like tds, roblox tds, how to make, tower defenders, wave based, wave attacks, coroutines
Id: R3AREWh14iQ
Channel Id: undefined
Length: 20min 21sec (1221 seconds)
Published: Thu Dec 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.