Obby Ideas In Roblox Studio #9: Projectile Obstacle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you'd like to learn how to make projectiles for your Obby that fly out characters and hurt them if they get hit keep watching and I'll show you exactly how to do it welcome to Roblox Snippets and uh this is our ninth obstacle uh that I'm showing you how to do today that I'll be adding to a main game Obby at the end of this if you're interested or been following along and today's will be projectiles uh ones that will move towards the player like big Cannonball and if they hit the player they'll damage them uh there's lots of things you can do with that now in the demo I showed I'd made sort of a cannon sort of thing uh we're just going to keep it simple in the example and then you can take the code away and uh create a much fancier Cannon or or gun to fire them out of so we just need a couple of parts to do this um just to simulate it and the first one in our workspace if you come up to home and then over to par We'll add in a cylinder and we'll also just while we're here uh we'll add in a sphere as well so just drop down and add in a sphere and we'll just name there so we're clear on on what this is we'll call our sphere here a pod and i' I've used pod uh you could call it bullet or or projectile or whatever you like um because a pod sort of U I think from Plants versus Zombies uh so like a pod shooting out uh so you can make it whatever color Etc you'd like but I'm going to make mine green like plants versus zombies um and I'll just pick a a lime green and I'll make it a a neon color so that it's uh really bright and we'll come down and we might make uh the Pod um we'll make it fairly big I think um so that uh it's it's big enough to sort of run into a player because that's how I've designed it in my uh main game Obby so we'll just come down to its properties down here and we'll change its size to be 3x 3x3 and while we're in here if you move down uh to can Collide uh we're going to actually turn that off because uh we we don't want to run into the player although you could by all means we're going to uh destroy the Pod when it hits the player so I'll turn off can CID but B mans leave yours on if you wish and anchored I'm going to turn on the time being because we want it to stay wherever it gets spawned and then we're going to move it so that's our pod or our bullet that we're going to use now we'll create this other part over here and we'll call this our Cannon right you could call it gun if you wanted to and we'll just make a couple of changes to this because um we we don't want to spend too much time building uh first thing will'll be anchored we'll put on we'll turn off can Collide and then we'll come up to the size for uh our Canon and we'll change this one to be uh three by 3 by three as well and just move it up like this and now we want it to shoot in a Direction so if we uh come up to rotate and we might uh click on model and we'll choose 90° here to make sure that we only move at the amount that we want and let's let's rotate it so that it's firing sort of out in front of us like so um which you can make a fire any direction you like and we'll make it a bit uh bit more like a gun sort of color say a dark stone gray and maybe change its properties down here all right yeah let's just go metal that'll do all right so we have these two parts and I'd say what we'll do is we'll move our pod part for a start because we're we're going to make this sit basically inside of this part here all right so we want to move our pod inside so if we take the click on the cannon and then come down to its position currently click on it and hit command or contrl C to copy it and then we will click on the Pod and we'll paste that position into its position and hit enter and you should find find that it will move directly right inside of our our cylinder here now I'm a little bit off in uh which way we're facing when we started this so I'll just um I'll leave it where it is at the moment and uh we'll see how that works so we've positioned it in the middle of our Cannon so it's a loaded gun ready to go right so if you've got something looking like that we're ready to move on and add some code to it all right so the first item that we will add code to is our pod now you'll sort of notice like I said uh this angle is a bit off I'd like it to be a bit square and I thought I'd leave this in just so I could show you if you wanted to fix these things if we click on our Cannon you'll notice that under its properties under position there's orientation and it's it to this 68 now if I want it to be square I can change this to be uh 90 and then that will line up perfectly with my base plate and square on uh when we drag the other item forward now if I click on the pod all right you'll see that its orientation is set to zero so now we can drag it forward so that it comes out of the cannon um smoothly without having to move sideways all right so as I said we're going to add some um code to uh the Pod so you can move it outside of the cylinder at the moment but click on the pod in the workspace and we'll add a script to this and we're going to call this our damage player script so over in our script let's get rid of Hell world and we'll add a couple of variables and a little bit of code so create a variable for the Pod itself so pod equals script. parar and then right below this we'll add a variable called DB short for debounce used to control our code and you'll see how that works down below now I've already written the script so I'm going to create another variable here called damage uh per hit which will be equal to 10 and you can play with this figure um you could make this 100 or you could even just kill the player straight away if you really wanted to uh totally up to you down below this so when the Pod uh gets touched all right so that's the event that's happening we're going to connect a function to it and pass in whatever hit the Pod and in this case we're looking for a character so we'll create um uh we'll create a a variable here for a character and we're going to assume that what hits this um hits the Pod is going to be a character but we'll write a conditional statement to check uh because there may be other things in the game moving around um like the or it may hit a building or something else and we we just want it to do damage when it hits a character or player so for that reason We'll add the player in as well so local player and we'll get the player by saying game. players and then get player from character and pass in our character now with that information we'll make a conditional statement so if we have a player and DB then we'll immediately set DB to be false and towards the end of the script we're just going to put in a task. we and we're not actually going to um set DB back to true we're going to destroy this pod so just below this task. we we can add pod and then a colon and Destroy and that will destroy it for us so now in here this is where we can do the damage uh to our character or specifically to its humanoid so we create a variable for the humanoid and then we will look inside of the character so find first child and we're looking for the humanoid once we have the humanoid we can simply put in here humanoid take damage and remember we created this variable up the top here damage per hit so we can type that in and add it in now if you wanted to kill your player uh you could simply put humano and set their health and set it to zero and that would just cause them as soon as they head by the Pod to die however we're not going to do that in this case so I'll take that out and that's all we need uh in order to get this to work so when you've done that uh we'll get the cannon working uh before we sort of test this out so close that script down and we will come up to our Canon now and add a script so click on Canon add plus na script and we'll change the name to be Canon script and get rid of our Hollow world over here so what we're going to do is we're going to spawn the pod which we're going to keep in server storage down here so each time that we want to fire a pod we'll we'll get a copy of it from server storage and then we will fire it from inside this script so for that reason we'll create a variable called SS for Server storage and and then we'll get that by saying game get service and in inverted commas server storage so that will get us access to this folder down here and although we haven't moved the podt in there yet let's create a variable called pod master so this is like the Master Copy that's held in server storage so equals Ser storage and then we will wait for child for the name of what we call this over here so make sure it's named exactly the same in this case it will be simply pod and we'll take a copy of that to fire out which remember it's got this damage player script attached to every time it is spawned so we don't have to worry about that now the way we're going to move our pod is to use the tween service so we'll create another variable here TS short for tween service and type game get service and our tween service so twins will let us uh animate the Pod and move it from one spot to another smoothly and we can do that over and over and over again uh once the tween is created we will however um I don't we won't need um a debounce in this case uh because we're going to create this with the inside of a while loop so just a couple of lines down below here we'll add a while loop by saying while task do we and then just outside that do and drop down line now we don't want it to fire continuously like this we'll save the time between shots in here will be how long we want to wait for all of the code to run so let's just create another variable here called time between shots and let's set it to say three all right so the lower the number um the more shots that will will be fine the higher the number less time all right and we can put this in here so time between shots and this will run this every 3 seconds effectively all right so now that we have this Loop ready to go here uh we need to get a copy of the Pod Master up here so we'll create a variable called pod and it will be pod Master with a colon and we will clone that so which makes a copy once we have that copy we're going to say pod. parent will equal and we will just put it into the workspace uh you could have a folder if you needed to but workspace will be fine and we're going to set the position of our pod once we um work out where that's going to be so let's uh write this in for a start so pod. Position will equal a vector 3. new and for now we'll just put in 0 0 0 0 because we need to come back to our workspace and let's start where we want this to start so imagine you have placed this cannon exactly where you want it to file all right we can use the same method we did before where we take the cannon and come down to its position down here and copy it so command or control to copy and then we're going to paste this in here so that it's in the middle now this works uh you can just use the cannon um to position every time we spawn but you may want to the Pod to be back here or be loaded so um we'll we'll do the script this way so now that the Pod is in the cannon loaded we actually want to take its position so take a copy of that and come in here and we're going to paste it inside here so this is effectively its starting position so you could add a comment here starting position and I'll just get rid of these windows now we will set uh we'll create a variable below this for the Pod speed this will be how fast the the Pod moves from its starting position to whatever position you want it to finish in now keep in mind that if if I just come over here not not very well there we go right so if for example you only moved from here to here and it's taking 2 seconds then the speed is going to be very slow all right whereas if you move it like 400 studs out the pot is going to move very fast to try and get to that position within 2 seconds so hopefully that makes sense and I'll just undo that and put it back so you'll have to play with that figure a little bit to get it to work exactly how you like it but in here we're just going to make ours 2 seconds all right and now we can get on to creating our all the information we need for our tween so tween need uh some info and we do that by saying info equals twe info. new and it comes up with all these numbers now the first one in here here the the time is our pod speed so that's how long the tween it's going to take to complete the tween then add a comma and we'll add enum do eing style do and we'll just pick Linear by all means experiment with this and try others if you wish and then add another comma and you'll see in the helper text easing Direction so enum do easing Direction this time and add a DOT and we're going to choose this one up the top in out and you can try the others if you wish continuing if we add another comma Now it says repeat count so do we want this to repeat well no we don't okay because uh each time a pod is created we just want it to Tween from where it started to where it finished and when it's done if it hasn't hit anybody we want it to disappear so we'll put in here zero so it's looking for a number right so not true or false but a number if you did want it to repeat endlessly you could put minus one and that will continue to do the TW over and over or you could put in a number here such as I want it to run three times but for now zero is the number we're using and do we want it to reverse in this case we don't all right we don't want it to reverse we just want it to go one way so that is all of the information that we need for our tween info now if you missed that to make it a bit easier after each comma I'll just hit enter so that you can read exactly what is going on and that will still run the same um it doesn't damage The Code by putting it across lines like this doesn't look very good but it makes it easier for you to read all right so once we have this info right below this we'll now create a variable called goal and put a set of squiggly brackets there and this represents where we want our pod to move to so the twe will take what we put in here and apply it to the Pod up here in this case we're going to change the position all right so inside of here we can say um goal do position and we'll equal a vector 3 uh sorry that's I put it inside of the table here you can do it two ways you can do it this way where you just put position inside of the table like this but I I'll just uh rather than confuse you so this has an empty table and so now we'll say goal do Position will equal vector 3. new and now we need to decide where it's going to end up so coming back to our workspace let's say um our pod has started here and we're going to move it let's say 80 or 100 studs out so if I change this to be uh say 20 at a time and we move our pod so we go uh 20 40 60 and 80 100 all right so it's going to move out 100 studs to complete the tween from the cannon so now we can come back to our script and I'll just bring up our exploring properties and let's have a look at its new position so we'll take a copy of this new position here so clicking in the pods position and copy it and then we want to paste it in here now you'll notice that these two are exactly the same except the Zed Has Changed by 100 so if you knew which direction it was moving you could have just put a one in there and made it 100 studs all right so hopefully that makes sense now that we have this uh finishing position so and you can add a comment in here so say finish position and I get rid of those we can create the tween because now we have all of the information that we need so right below this we'll say local twin will equal TW service and a colon and create whoops not clone create and you'll see that it asks for what is it that you want to Tween now it is the Pod so that's the part that we're interested in and then a comma TW info which was the info table we created and the property table is our goal table so we've given it everything it needs the last thing to do is to type in here twin and a column and say play now after the the twin has played we want to destroy the Pod but we can't just put destroy here because the twin takes time to to play so we use a method by saying tween do completed so when it's finished we're going to connect a function to this so brackets and just move outside and hit enter we can say pod destroy and that should be everything we need to give this a test now what this will do is once the tween has reached this destination the Pod will destroy all right so we can give this a test uh before you do that come up and make sure under view you have your output window on so you can see if you have any errors give it a save and let's click on play all right first error uh was by me user error okay and this can happen to you too is we didn't put the Pod into the server storage for it to make use of all right so let's open up uh our Explorer and properties now come over to your pod if it's in the workspace like mine and right click on and choose cut and then come down to serice storage right click and paste into and now we should be right to give this another go so let's click on play right if all has gone well you should see that your Cannon is firing out pods every 3 seconds all right let's check by standing in front of it and you should find our damage script is not only getting damaging the player but it is destroying the Pod when it hits a player but if I step out of the way the Pod will continue and destroy itself all right so if that's enough for you and you can um you want to go and put it into your own project I wish you luck otherwise if you want to stick around I will show you how I add this to my main game Obby um in the big game that I'm creating so I'll all right if you've stuck around then we're going to transfer what we done here today into my main game Obby um and if you just want to watch uh if you're following along that's great uh if you just want to watch to see how to add it to your own project uh that might be useful as well so what we'll do is I'll just close this script here we're going to take both of these items and we're going to save them to a file on our computer so we'll start with the Pod if we left click on it click on right click on it and come down to save to file that will bring up uh this window and you should pick somewhere where you can find it again I've created this folder called video resources now I've already done this once so I'll just click on this one here you'll notice that I've put two letters in front of this um and that's short for Server storage so I can know where to put this when I add it to the main game and it should be called an rbxm file down the bottom and when you've got all that sort of typed in ready to go you can click on Save and I'll replace mine and then we want to do the same process for the cannon that we wrote in the workspace so we'll right click on it come down to save to file and this time you'll notice I put WS for workspace so I know where to put that it's also rbxm file and when you've got that ready to go click on Save and I'll replace the one that I had there right so if you want to open up your own project or the one you've been following along uh with me then I'll pause the video and I'll get that one up and running and I'll show you how to add it in right so this is the main game Obby that I've been working on and at the moment has lots of obstacles in it it's still pretty boring because it's just in a straight line but I'm going to be working on that um very shortly to get uh a little bit more interest into it but for now uh rather than go through everything that's here let's look at how to add this obstacle in so as I said uh the first one was our server storage down here so if we left click on server storage then right click on it and come down and insert from file and that should bring up the same folder that you had before if uh you haven't stopped working otherwise located on your computer and we're after the Pod to go into the server storage and we want to click on open all right and then we'll come up to the workspace and right click on it and come down to insert from file and we'll add our canon in like such all right so let's just zoom in on the Canon part for a moment and decide where we're going to put this so if I take this Cannon and I'm going to put this into uh the game sort of in a position but it may change in my main game Obby uh just for the purposes of the video we will just place it up here now I have here uh if you haven't seen the video before a hidden path that comes out here so what I'll do is I'll make that visible just for the time being select all of those and we'll simply make it uh visible so that I can place my Cannon um down the end here and we'll take the cannon part and I'm going to move it down Behind these boxes so that it is sort of lined up uh down the middle there I guess uh we might move it up a little bit and of course you can place yours wherever you you like all right so that looks like it's it's pretty close uh maybe over this way a bit yeah we'll just move over that way a little bit now you you might remember that in the example video we needed to position our pod so rather than um what we'll do is we can take a copy of this pod here so if you copy this so right click and then copy and come up to the workspace and paste it into the workspace we going to just uh maneuver it up to where the cannon part is up here so if I click on the cannon and get its position where I've placed it now so copy this and then I'm going to paste it into the Pod so we will simply paste and if I come up to the cannon now the Pod is placed right inside of the cannon now we'll take that uh position if we copy that and we want want to come down to the the Pod inside of server storage and open up uh actually we don't need to do anything inside of there that all works we want to go into the Canon my apologies in the workspace and remember we have this starting position here that we we made a comment so we want to put the starting position in here and then down below this now we want to change this position to see where it will end up so if we grab the Pod that is in the workspace at the moment and we move it along the line that we wanted to fire now you may not like my players get up to here but I I don't want them to get hit so what I'll do is I will make it come to this position here and then it will get destroyed all right so we'll grab that position from down here and copy that and add it into our Canon script in the finish position down here so we will copy that in and I just noticed I spelled position wrong all right and that should be everything that we need to do in order to get this to work now we don't need this pod that is in the workspace anymore so you can right click and simply delete it if uh you have it down inside of server storage so with that uh if we come up to file and save and let's play all right off in the distance up here you can see that the cannon is firing along uh my Obby but I will pause the video and then I'll get up to there so you can have a better look okay so I'm up to where I can see these boxes out here and this hidden path if you haven't seen this before you have to get up to this spot to this box without dying in order for the path to show up so players could get tricked if they get up to here thinking they can jump on top of these balls to try and get out there and that's not a bad thing to have in the game but if I touch this box you'll see no path for you down the bottom here and I can't actually get out to those boxes however my Cannon is working correctly and I hope it is for you and in future videos I'll show you how I make better use of this um in the uh the Obby that I'm creating so I look forward to seeing you in future videos If you found this video useful subscribe now for more information about my online courses go to Mr Brandon ross.com
Info
Channel: Roblox Coding Snippets
Views: 36
Rating: undefined out of 5
Keywords: roblox how to script, roblox how to script for beginners, how to script on roblox, how to script on roblox 2024, how to script on roblox for beginners, how to script on roblox studio 2024, roblox beginners guide to scripting, roblox coding tutorial, roblox coding tutorial 2024, roblox scripting basics, roblox scripting for beginners, roblox scripting tutorial, roblox scripting tutorial 2024, how to code on roblox, Obby Ideas In Roblox Studio #09: Projectile Obstacle
Id: PRm5u9U2qGo
Channel Id: undefined
Length: 29min 36sec (1776 seconds)
Published: Fri May 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.