so you're opening up studio with a great
game idea ready to make it quickly and
hit the front page just to start
building a map and realize you really
don't know anything about how to get the
game to actually work then maybe you
searched up how to script on
roblox tree now vacuum tree is you were
extremely confused by all the bubbly
jumbly mumbly jargon in all the videos
and just ended up spamming your keyboard
on your script
before picking up a hammer smashing your
computer and kicking your monitor in and
slowly throwing your computer out a
window and driving your car
it's safe to say that might have been
the last day for some of our computers
but don't worry about it because i am
here to help because i was in that
tragic situation just a year ago but i
managed to learn scripting eventually
after starting as a builder and having
barely any idea what i was doing just
like you this guide is going to explain
you how i learned a script in the
simplest way possible and i can
guarantee you'll be able to script by
the end of this video you're going to
need to open up roblox studio of course
and we're going to make a base plate
just a normal base plate and we're
actually not going to make a script
first we're actually going to build
something for well we're just going to
place one part down but it kind of
counts so i'm just gonna delete the
spawn location there let's close that
tool box we aren't gonna be needing that
for now so just click the part button up
here and we're gonna scale this part up
just a little bit and i'm gonna color it
here so it's just a pink part and we're
gonna name this part pink bark with a
capital p on both of those word now so
make sure to pause and bounce around
this video by the way parts that you
might have forgotten when you need them
this is going to have a ton of chapters
so you can bounce around the timeline
it'll all be cut up to all the different
definitions and everything so it's
really helpful and useful for you now
we're going to right click this part
click insert object and we're going to
add a script into this which is at the
very top usually or you can just type in
script you don't see it there now we're
going to open up the script and now
we're just going to delete everything
that's here and we're going to type in
local
part equals script dot parent now the
first thing we're looking at is
variables and what we have just written
out is a variable a variable just stores
a value so the variable kink part stores
the value script apparent which is well
the parent is just whatever the script
is inside of so that would be the pink
part so that's what this variable equals
this variable equals pink part by the
way make sure that you write this
exactly as i did a lot of things in
scripting are very case sensitive so
it's very important that you write this
in how i wrote it with the uh uppercase
and lowercase it all makes sense
eventually but a value in a variable
could be anything it could be an
instance which is just a object in
roblox studio like a part or gui you
could also make it a line of text which
is called a string in scripting or you
could even make this equal to a number
so now let's make a new part but wait do
not close this script we're gonna make
this part using scripting so what we've
done is we've already made a part
normally and we defined it through our
script with a variable but now we want
to create a part with our script so what
we're going to write is local part
equals instant.new which makes a new
instance and then we're gonna type
parentheses and we're gonna type in a
quotation mark and then we're gonna
start typing part and click enter now
i'm gonna use my arrow key go out of
that click enter all right remember case
sensitive now this variable is telling
the script to make a new part and we can
basically make any kind of object in
roblox studio with this too but this
part doesn't know where to go right now
aka what object it's going to be
parented to and again the parent is just
whatever it's inside of so right now
we're going gonna parent this object
this part to a different object in
studio now right now the parent of the
part is nothing and in scripting we
write nothing as nil so part dot parent
equals nil that's already what it's at
so if we change it from nil or nothing
to let's say game which is just the
whole game that we're editing dot
workspace so everything inside of the
explorer here is in the game and inside
the game is workspace at the very top
which is where we see all our hearts so
this part is going to go in a workspace
and it's going to be visible in the game
when we run the game it's not going to
be visible until we start playing the
game parent is actually just a property
of an object in studio and a property is
just one of these details on a different
object in studio so it's like color
shadow cast shadow color material
material variant reflectance transparent
these are all properties of a part so
what we're going to do is we're going to
change another property of this part so
we're going to make a new line just by
going to the end of this and clicking
enter a few times and we're just going
to write part dot transparency equals
0.5 remember to use that auto fill you
can start typing something and just
click enter and right like that it will
build the rest of it in it's really
useful make scripting a lot easier now
let's go to the top of roblox studio and
click the play button and let's test it
out and there we go so we had a new part
fall right here um this is a 0.5
transparency part and yeah it's a pretty
cool example of how you can use
scripting to make a part in studio all
right so now we're going to stop the
game we're going to go back
congratulation on your first script now
you can feel free to pause the video and
mess around with any other properties
you want with your script but right now
we are going to be moving on to looking
at boolean and booleans are basically
any checkbox in studio is actually a
boolean so as we can see here cache
shadow which chooses whether or not this
part has a shadow on it as you can see
when we turn this off and on switches
whether or not this has a shadow now
actually what this means is hash shadow
equals true so when it's checked cast
shadow is true if cast shadow is
unchecked i mean cast shadow is false
that's just how we write it in the
script so cast shadow true cast shadow
falls so if we go ahead and make a new
line and write heart dot cast shadow
equals false then this part will not
have a shadow if we change this to true
it's going to have a shadow so we'll
leave that at false and we can mess
around with a few other properties so
part dot anchor equals true if we do
that then it means that this part is
just it's not going to move or anything
we're going to be able to push it around
with our character probably already know
what anchored means it's going to be
stuck now one other thing we can do in
studio is we can make the script wait so
we could actually go up in the script a
little bit and we can make the script
wait to make an anchor so if we type in
weight and then we type in a number and
by the way numbers in studio do not have
quotation marks but strings do strings
are the lines of text numbers do not
have quotation marks so just remember
that whenever writing a number down you
do not put quotation marks now this is
going to wait five seconds before it
makes the part anchor so all the other
code before it is gonna run and then
it's gonna start waiting for five
seconds and it's gonna anchor the part
now this is a little bit more advanced
but to make this a little bit better for
your performance on your game there's
actually a new feature in studio that's
called task.wait and you'll usually want
to use task.wait if you aren't doing
something that needs to be very exact
weight makes it exact and it forces the
game to wait that long but task.weight
basically will let the game work at its
own pace it just might be like 0.001
seconds off i usually tend to do test
out weight when i can because it's just
a little bit faster alright so now most
tutorials would have actually shown you
this first but let's take a little bit
more of a look at strings so something
that you can do with strings is print a
line of words in the output which is a
window that spits out warnings errors
and prints from your scripts and roblox
studio itself so if we go ahead and type
in print and then we add parentheses and
then we can type in a quotation mark and
this is going to make a string for us so
we can go ahead and write hi and then
when we run the game by going to test
and click on this drop down on play and
go to run now in the output of studio
after five seconds the weight in our
script it's gonna say hi down here now
that's pretty cool and we can also print
numbers so we go ahead and print five
the same thing will happen this will
work just fine and by the way you can
put numbers in strings so if we said
high five this is still going to work
when you're writing a number by itself
you don't put quotation now this is
mostly useful for when you're trying to
debug your code and you want to mark
where certain things happen so like
after
the part is anchored we could put a
print and we can say
part anchored now every time that
happens we'll know the part was anchored
and if we do not see this in there we'll
know that for some reason maybe there is
an error in our code
the part did not get anchored and that's
what you're going to be using print for
most of the time now in scripts we can
also do math and we can print numbers
and strings in the output and we can
also print math in the output so we can
actually print three plus three let's
see what happens when we press run on so
after five seconds again after our
weight is over that's going to do this
see it prints out six three plus three
equals six so it's going to print that
out now make sure you do not put
quotations around this just like i said
because we are writing a normal number
here and this is not a string and it's
really important that you don't do that
when you're doing math we can do a lot
of other things uh we can also do
multiplication and to do that we can
just do a asterisk or a star little star
it should be on the 8 on your keyboard
you just hold shift and press eight and
you'll get that star and if you do three
times three well it's gonna print out
nine and we can do this with the
division as well so if we change the
star to a slash it'll divide three by
three and we'll get one and there's a
lot of other cool things that you can do
in math in roblox studio but we're gonna
get back to this later because now it is
time to move up to the next level this
is where things start to get intense
well not really just focus and follow
along this is where you're gonna be able
to start making things really work in
your game and it's going to be
incredible so make sure you do not click
off if you want to become a scripter i
see that mouse of yours inching closer
and closer to the youtube homepage don't
do it don't give up on your dream being
able to script your own game because
this is well worth it and it could end
up being a real money maker for you
later now we're on to function functions
are exactly what they're called so i'm
going to go ahead and just click control
a and delete everything so we can start
with a fresh slate basically functions
are just a piece of code that performs a
function now and this is just an action
in your code and it's when you call upon
it so basically we don't want to be
sitting there just typing the same code
over and over and over and over and over
again so that's where functions come in
and they have a few other cool things
you can use them for too functions can
be called upon with just a single word
and it will run your entire function
your entire chunk of code that's in that
function without having to rewrite it or
anything all you need to do is call the
function by writing its name and
parentheses after it but there's also
another neat feature let's say you put
something into a function well you can
actually put a value into a function and
it can spit something back so you can
have a function that adds two to
whatever the first parameter or input in
the function is and it will go ahead and
print that so it'll add two to whatever
you put into it and it'll print it a
parameter is basically just a variable
when you're using it in your function
and trust me this is going to be a lot
more useful later and it's also going to
make a lot more sense when we actually
write this out so don't worry we're
going to make a function right now so
let's go ahead and write function add 10
x so x is going to be our first
parameter so that's what you're going to
do when you call the function you're
going to input x which will be whatever
number that you want to add 10 to we're
going to go ahead and add parentheses to
that and type x again and we're going to
click enter and we're going to write
local y equals x plus 10. so what this
does is this makes a variable y which
equals our input x plus 10. that's what
it's doing so it's making a variable y
and it just adds 10 to that and that's
what y equals y
is just x plus 10. now what we're going
to do is we're going to print y so this
is going to print x plus 10. so if x
equals 2 and we add 10 to it that's
going to be 12. so what y is going to be
is 12 and it's going to go ahead and
print that and then what we're going to
do is we're going to return y and what
this will do is it's going to basically
wherever we call the function we're
going to be able to use y later in the
script also what i did right here is i
wrote a comment you just put two dashes
and you can write whatever you want and
the script is going to ignore this you
can write notes and it's pretty good
practice to just write these wherever
you can so you remember what things do
in your script and also it helps if
someone else is going to be looking at
your script later down the line so
functions are one of the most powerful
things in scripting and one of the most
important pieces of functions is called
scope and you might have noticed so far
that i write local before basically all
variables well you can also write local
before functions and if you don't write
local that means it's naturally a global
function so a global function can be
used throughout the whole script no
matter where it's written a local
function can only be used after it's
been written or if it's ins if it's
inside of a function you can only use it
inside that function so we can only use
y here inside this function unless we
return it and also if we write a local
function at the end of the script for
example we can't use it anywhere at all
same for functions if you wrote a local
function at the end of a script you
could not use that function anywhere
unless you change it to a global
function so this is going to be a lot
easier to understand after we practice a
little bit more so if we want to go
ahead and use this function that we've
written here we can actually type in add
10 and we can type in right here in our
x value so let's write 2 so what it's
going to do is it's going to print 10
plus 2 which is 12 and it's going to
return that for us so we can actually
write local number equals add 10 to and
what this is going to do since we
returned y this is going to equal y this
variable will equal 12 and we can go
ahead and use that throughout the rest
of our script which is why this is so
useful especially when you get into the
more advanced sides of scripting so
hopefully you understand functions but
if you don't don't worry we're going to
get a lot more practice in after this
this is just a brief explanation and
again feel free to rewind re-watch any
parts of this video that you need and
remember do not give up you are well on
your way to becoming a grit scripter and
don't worry if you don't understand
everything yet this video will clear up
all your questions by the end so next
thing we're going to do is we're going
to look at if statement if statements
basically check if something is true
and if it is it does it so okay so we
need a variable for this part so local
part equals script dot parent which is
our pink part so local part equals
script.parent and what we're gonna do is
we're gonna do if part dot anchored
equals equals equals equals basically
just means that it's checking if
something is true you need to do that
whenever you're checking if something is
true if part.anchored equals equals true
then part.color equals color three dot
from
rgb 255 zero zero so what this is gonna
do this is just uh color three is just a
three number value that that makes a
color and if it's from rgb it's gonna be
making it in red green
and blue and the highest number being
255 lowest being zero so if we write 255
on the red it's going to be a fully red
color if we were to add a hundred of
this it'll make it a little bit pink
since we're adding a lot of blue if we
added a lot more like 220 it'll make it
more purple since
red and blue usually makes purple
all right so what this is doing is it's
checking if the part is anchored and if
it is it's changing the color of the
part to red now we can also add to this
and we can make it check for something
else so if this is not true it's just
going to skip the changing of the color
of the part altogether but we can make
this move on and say
else so
if that isn't true it's going to move on
and it's going to say print heart is on
anchor so it's going to print that the
part is unanchored so if it's anchored
it's going to make the part red
otherwise it's going to print
part is on anchor and honestly this is
one of the most useful things i use all
the time in scripting because there's a
lot of times we need to check things are
true check if things aren't true check
if things exist at all in your game and
we don't have to just check if something
is true we can actually check if part
dot color if part.brickcolor equals
brickcolor.new and then oh yeah we got
to do the double equal signs i'm
forgetting too it's easy to forget if
part.brickcolor equals medium stone gray
then we can print this is a gray part
and this is going to write in the output
that this is a gray part as you can see
this doesn't even have to be a whether
or not this is true because obviously we
can't have it if heart.brick color
equals equals true but if the parked up
brick color equals brickcolor.new medium
stone gray then it's going to print that
this is a gray part and otherwise it's
just going to skip over that code just
like all if statements now there's also
elsif statement so what we've looked at
so far is if statements with check if
something's true and if it is it does it
we looked at else statement which it's
just moves on so basically if that isn't
true it'll move on it'll do whatever
else is in the script but what else if
does it's like else but then you're also
checking for something else so if part
dot anchored equals equals false then
it's going to print part is unanchored
so this already it does the same thing
but it's checking for something specific
so if we did this on something a little
bit better of an example is doing it on
the brick color so else if part dot
brick color equals equals brick color
dot new re really red it's going to
check if this is medium stone gray and
if it's not it's also going to go and
check if the parts color is really red
and it's going to print in the output
this is a really red part so as you can
see this can be very useful so if
part.brickcolor equals equals
recolor.new medium sewn gray then it'll
print this is a gray part otherwise it's
going to check if this is a really red
part and if it is it's going to print
this is a really red part otherwise it's
going to skip both of these things all
together and that is why else if
statements else statements and if
statements are all just as useful
there's just different scenarios when
you're going to need to use them when
you need specific things done in your
script so now we are just going to
completely ditch this script for now i'm
going to close it out and i don't want
the script doing anything for now i
don't want to delete it though so right
right now i'm just going to actually uh
right click rename this script to not
not using this lol and i'm just going to
click disable in property so the script
is not going to do anything and we're
going to make a new script by right
clicking this part clicking insert
object and clicking script and it's
going to go ahead and make a new script
so let's clear out the script by
clicking control a and just clicking
backspace and we're going to write if
script dot parent dot name equals equals
taco then print taco elsif script dot
parent dot name equals equals hamburger
then we're gonna print
hamburger so what this is gonna do is
this is another if and else if statement
and this is going to check if the parts
name is taco and if so it is going to
print taco if it is not taco it is then
going to check if this is a part named
hamburger otherwise if it is not named
hamburger it's going to do nothing if it
is it's going to print hamburger else
will always do something if the first if
statement is not true else is always
going to do something so if we do else
it's going to print hamburger no matter
what no matter what the name of the part
is if it isn't taco it's always going to
print hammer but if we add on that elsif
it's going to be checking for something
specific and it's going to print
hamburger only if the parts name is
hamburger so hopefully that clears up
the difference between else and elsif i
know it's kind of hard to get at first
but it's very important that you know
the difference because you're going to
be using both of these a lot in script
all right we're done with that enough
with the else and elsif so next are
events and events are really cool events
are really interesting because we can
finally start to check and like
listen for when things are happening in
our game and we get to have script that
run when something happens in your game
so let's say a player touches a part we
can have an event of when a player
touches a part we can run a chunk of
code when that part is touched so what
we're gonna do is we're gonna go ahead
and make a new new part we're going to
make a kill brick over here so i'm going
to go ahead and add a part anchor that
thing scale it and let's i'm going to
make this you know a nice little red and
we're going to add a neon to that nice
little kilbray cool looking i want to
turn off cast shadow on that too just
for the heck of it and i'm going to
click the plus on the part and i'm just
going to add a script into this by
clicking on it and i'm going to clear
the script out by clicking ctrl a
clicking backspace as usual and i'm also
going to name this script i want to name
this kill part script so first we're
going to define our variables and we're
going to write localkillbrick
equals script.parent so what we're going
to do is we're going to add an event
that basically it's gonna check when
some other part touches this part and
then we're gonna check if that part is
inside of a player's character we're
basically gonna check and listen for
when a player touches this part and then
we're gonna go ahead and kill that
player's character so we're gonna go
ahead and do that so we're gonna listen
for the touch event and we are going to
make a function that passes on the touch
part which is the part that touched the
part and we're going to write that as a
parameter the touch part parameter is
going to pass on to the function and
it's going to tell our script the exact
part that touches so we're going to go
ahead and make this function so kill
brick dot touch which is the touched
event and we're going to write a colon
connect click enter and we're going to
type in function and then add
parentheses touch part and going to go
to the end of this we're going to go to
the right in between these two
parentheses and we're just going to
click enter there i'm going to click
enter three times just so i have some
space and so what this is going to do is
as i said it's going to listen for the
touch event pass on the touch part
parameter to this function so we know
what part touched this part and it's
gonna tell our script the exact part
that touched it and now we're gonna do
the killing of the humanoid
so we're gonna do local humanoid equals
hutch part dot parent so whatever the
touch part is inside of and we're going
to do colon find first child and find
for style is a built-in function in
roblox studio which we can run on any
object inside of the game and it will
basically just loop through and check
each child of an object in studio and it
will check whether or not an object with
a specific name is inside of that part
so it's going to find a child with a
specific name so we're going to find the
child called humanoid and a humanoid is
what controls all the aspects of a
character like its health how high it
can jump how fast it can walk everything
like that and what it's gonna do is it's
gonna see if we have a humanoid and if
it does it is going to say okay here's
the humanoid and this variable is going
to equal humanoid if there is not a
humanoid this is going to equal nil so
what we're going to do now is we're
going to check if there's a humanoid so
we don't get an error if we don't check
if there's a humanoid and there doesn't
end up being a humanoid for whatever
reason like if let's say a part that
wasn't a character touch this part that
part isn't gonna have a humanoid if it
touches it it's gonna error out if we
don't have this if so that's why we're
doing fine first child it checks if
there's a humanoid and now we're gonna
do an if if humanoid it's all it is if
there's a humanoid and basically by the
way nil and false are the same thing
writing nil if nil that's just like
writing false so it falls basically so
if this is nil it is going to not run
this code but if there is a humanoid
it's going to run it so hope that makes
sense so if humanoid then
humanoid.health equals zero and that's
it so we're going to go ahead and play
the game and let's hope this works so
i'm going to go back to my baseplate
click i have play here pinned up here um
and we click play here
so i play right on it and when i touch
this part it is going to kill my
character there we go so we have a
perfect aubie kill brick all made for
our game here that we're making and by
the way by the end of this video you're
gonna know how to make an obby did i
mention that we're gonna be making a
hobby yup you're gonna be making a game
in this first video you're gonna be
scripting your first game it's amazing i
don't want to get too cocky but i'm
gonna say this might be one of the best
scripting videos out there because i'm
gonna condense all the information into
one video and hopefully you will be a
scripting pro by the end of this video
that is my goal so that was pretty easy
right now a lot of things in studio
built-in event so i'm gonna open my
script back up and these built-in events
we can actually see them with the object
browser in a nice list so if we go to
view
object browser this will open up a new
window and we can flip around these and
this will basically show all the objects
in roblox studio and everything that
exists so if we choose if we can find
part in here now i wish there was a
search on this that's the only thing
there's part okay if we find part in
here we can actually see so in here the
lightning bolts are the all the events
on the part so we can listen for all of
these events not just touch we can
listen for touch ended so when something
stops touching it or when the part is
getting destroyed when a child's added
when a property is changed there's all
kinds of events on different objects in
studio and every object in studio has
tons of different events that are unique
to it so there's a lot of cool things we
can do with events and it doesn't just
have to be touched we could listen to
any event we want so we could say touch
ended and when something stops touching
it's gonna check if that part has a
humanoid and it's gonna kill it too now
i'm not sure if this always works i
don't think this is that reliable but
again there are there are ways that you
can do that that's just an example there
are a lot of events and we can we can
use these for a lot of things by the way
quick reminder you are doing great you
are getting really close to becoming a
full-on scripter and you got to keep
going do not give up so we're getting on
to the next detail which is random
numbers random numbers i mean they're
self-explanatory but they're really
useful for a lot of things and
especially for waiting a random amount
of time so for example if we want to
wait a certain amount of time after the
player touches this part to kill them
we can do
of course task dot wait just so we have
that good performance benefit of using
task.weight versus weight and then we
can just do
math
dot random and then add parentheses then
what you do is you write the minimum
number here so let's do
task.weightmath.random
one second and then comma
10 seconds so what this will do is it'll
pick a random number between 1 and 10
seconds all right so after this i want
to take a look at loops loops basically
just let you repeat things forever or
for a certain amount of time or until
something is true there's a lot of ways
that you can write loops so one way is a
for loop so for let's go ahead and make
a new script i'm going to make a new
part anchor that and then i'm going to
hit the plus add a new script so the for
loop as i said so the for loop is it
will loop through a number or a table so
it will count up from one to that number
if we do four
number equals ten so it's going to count
up to ten repeat basically i'll just
repeat this ten times for number equals
ten comma one do
for number equals ten comma one comma
one do script dot parent dot size dot x
equals script dot parent dot size dot x
plus one so what this is gonna do is
it's gonna add one stud to the x size on
the part ten times and i'm just gonna
add a short weight between that so
weight zero point uh let's do weight
zero point five oh test.wait don't
forget that and we can go ahead and test
this out
oh actually you have to do number equals
1 comma 10. i was wrong um it's the
other way around and we don't need that
third one but if we go ahead and test
this it should work now oh another
mistake i keep making mistakes i know
the problem
script.parent.size we need to make this
a vector3 so vector3 as i said earlier a
vector3 is just a
three number
value used to show usually positions
with parts or sizes vector3 dot new
and then just add parentheses around
that and then the first values x then
we're just going to do y uh we'll just
copy this
script.parent.size.x and change that to
y same thing except we're putting z on
the last one and now it should work
finally worked out all the bugs and as
we can see it's working now it looks
like uh i made my weight a little bit
too short but as you can see it added a
lot of studs to each side and it
definitely grew by ten studs so that's
how four loops work in the most basic
way with numbers for loops can also loop
through a table and um so for example we
can do four game
get service players get players for i
comma v in pairs so what this will do if
we write this for i comma v in pairs
parentheses game get service players get
players what it will do is it will get
the players from the player's service on
roblox and it will loop through each
player and we can do whatever we want to
this so what v does is it will that will
equal the current player that we're on
and i is the table the table is just the
list of all the players we've gotten
from the player's service so if we say
vita character destroy now this probably
isn't going to work since we do have to
wait for the uh for the character so if
we do v wait for child character or if
we do v dot character added v character
added connect function
so what this will do is it'll destroy
the character every time it spawns now
obviously we don't want this but as you
can see this can be very useful to be
able to loop through a list of things
especially players like when you're
making a mini game system you might need
this or a lobby system it comes in handy
a lot so we definitely recommend
practicing this so that you can use it
when you need it because trust me this
is definitely something i use constantly
i make hundreds of these every time i
every time i script it seems like so
you're definitely going to want to know
these now next we have well loops and
with well loops we can just do well
something is true do something so we
could say well script dot parent dot
anchor equals equals true do as long as
this part is anchored it uh it'll repeat
this and it'll stop when the part is
unanchored now
a lot of people will just use well loops
for uh looping forever so well true do
so basically this will just make it
repeat forever because true is well it's
always going to be true and it's
checking if whatever this is is true
it's a little bit hard to explain but
basically it just means it's checking if
true is true and yeah true is true it's
always going to be looping so you can
have this loop forever now whenever you
do one of these i do recommend always
having a weight of at least uh nothing
so if you do that it'll do the shortest
possible amount of time that it can wait
and you need this otherwise roblox
studio can crash so or your roblox game
might crash too if you have a loop and
you want to keep these down and you
definitely want to be careful with these
because they can cause a lot of lag
especially if you don't have that weight
in there if we ever want a well true do
loop to stop we can actually add break
and what break will do is it'll just
stop it and it'll continue on with all
the code in front of it now we probably
do want to practice this um welter do
loop if you don't want to practice this
uh you can always skip ahead to the next
chapter on the video timeline but this
will be really quick so we're just gonna
make hearts rain out of the sky so local
part equals instance dot new
parentheses
part
part dot parent equals game dot
workspace
local
random x equals math dot random negative
200 comma 200 and then local random z
equals math dot random negative 200 200
and then we'll do throw all those
positions together into part dot
position equals vector three dot new
random x comma let's say 50 studs in the
sky and random
z
pass dot wait dude let's just do the
shortest one let's see how laggy this is
gonna be like a lag test on my computer
here
um let's go ahead and play this and as
we can see parts are raining out of the
sky so that was a mission successful and
that is one cool example of how you can
make welter do loops and use them
effectively in your game um yeah now i
promise we are almost done with the math
almost but i need to give you guys an
example for how to use a break so we
probably want this to stop running parts
at some point otherwise our computer is
going to absolutely crash
um well first we probably want to add
debris basically what this does it'll
automatically make the part delete
itself after a certain amount of time so
let's look at debris service up here so
debris is a service local debris equals
game get service debris and what we'll
do is we'll get debris
and then
add item part
lifetime so what this is is we'll just
make this last um let's say 20 seconds
for each part and now what we want to do
is we want to make this break if there's
400 total parts in workspace so so what
we want to do is we can actually just
add a part count we want to do that
outside the loop local part count equals
zero and then we'll do part count equals
part count plus one and if part count is
greater than or equal to 400 then break
we probably want to put this at the top
here all right so next are tables which
you might have seen that you've probably
seen these before visually or in a
school document but
these actually can be made in scripting
too but they're quite a bit different
but it's actually pretty similar to a
table on a document if you want to look
at it that way you'll see it has rows
and columns and this is very similar to
how tables work on roblox scripting we
use them to store large amounts of data
and then loop through them very quickly
with for loops which i just showed you
and they're extremely useful they come
in pairs so if we look at a visual table
we can see that there's a row and then
we could have a number right next to it
and there could be a value in the next
column over
this is called a key value pair a key
being the row number or the row
name
and the value is just whatever piece of
data that could be a string a number a
picture whatever
whatever piece of data is associated
with the key
also the key doesn't have to be a number
neither does the value as i said so
let's go ahead and delete this you don't
have to you can just make a new script
but i'm going to delete mine and let's
make a new table with a variable so
local we'll make a fruit table so local
fruit equals and then what we do is we
hold shift and then right above the
normal brackets like these right above
that if we hold shift we'll get these
this gives you a table bracket so just
go ahead and click enter a couple times
okay so the first uh key will be bananas
equals one and then you add a comma or
you can do a
semicolon after that so what we've
written here is that there are there's
one banana so if we go ahead and
reference this table later we see oh hey
table how many bananas are there it's
gonna save one it's gonna return one so
next we'll do apples
equals five oranges equals two and
that's it
so
the keys in this table are the names of
the fruit and it equals that number of
fruit there are so there's one banana
five apples two oranges and to access
one of the values in the table we just
write
fruit
at these kinds of brackets and then we
can write whatever name whatever name of
fruit we want which would be our key
bananas equals fruit bananas and then we
add we can add five to that
so what this does oh we do need to add
quotations around this i almost forgot
because it is searching the table for
that so this is just like find first
child which loops through a parts
children
but this rather than looping through a
part it can loop through a data set like
a table this is really just dipping our
toes in tables and there's a lot more we
can do we can loop through a table with
an in pairs loop and check for a certain
value like i showed earlier
and
find first child is a built-in function
a roblox studio that actually loops
through every child or part inside of
another part and it searches for one
with the same name that you input into
it
everything in studio is made up of
tables
literally everything like the children
of every part which are just the items
inside a part
even the properties of parts are pretty
much in tables this is why tables are
extremely important in scripting on
roblox or not so with that in mind we
can actually look through the children
of a part with the get children function
which returns all the children of the
part in a table for us so let's first
find some stuff we want to remove let's
first add some stuff that we can remove
so
let's add a bunch of particles to this
uh part here let's pour a part we're
going to add a particle emitter i'm just
gonna duplic duplicate that a million
times and i'm gonna go ahead and delete
everything we did here and let's do a
four iv in pairs loop looping through
all the children of this part and we're
gonna see we're gonna check if each
object is a particle and if it is we're
going to destroy it it's going to be
gone so
for i comma v in pairs script dot parent
get children
do
if
v
is a
particle
emitter
then
v
destroy the reason why we want to check
if it's a particle emitter with is a is
because we don't want to accidentally
delete the script that would screw some
stuff up so we want to make sure that
we're checking if each thing is a
particle emitter if you want to check if
something is apart for example which
you're going to need you can actually
just check if it is a base part and that
will check if it is a part of any kind
it could be a mesh part wedge doesn't
matter it could be a normal part it will
make sure that it is a part of some sort
and it helps because you don't have to
check for each kind of part you just
check for base part just need to throw
that in so i'm going to leave it as
particle emitter and let's go ahead and
play oh that's a lot of particles we
definitely need to get rid of those so
if we play the game and if they're all
gone we'll know it works and as we can
see it's perfect no more particles it
worked perfectly so this is definitely
one of the things i'm used the most in
scripting constantly i can't emphasize
enough this is one of the most important
things you're going to want to practice
this a lot you're almost there you are
almost a scripter do not quit now if you
click up the video you're probably going
to forget everything really really
quickly so we got to practice right now
let's practice by making a quick obby so
an ob is a good practice because we can
just make a bunch of obstacles do
whatever we want with that and we
already made the billbricks so why not
use that as our first obstacle so we
have this component so i'm going to go
ahead and start building an obby so we
can just add this in there scale it up
um and let's anchor this make it a
little bit darker and let's raise this
up and uh i'm gonna turn on my increment
to
one stud and then i'm just gonna
duplicate it a couple times and there we
go so i want to go ahead and make sure
we're organized by oh this is way uneven
there we go so let's go ahead and make
sure this is organized by adding a
folder in the workspace and i'm going to
rename this to kill part and just drag
them all in there and there we go so oh
this isn't the kill part put that back
in the workspace so now we do have a
problem we have all these kill part
scripts inside of each one of these
parts this is a kind of redundant so
what we're going to do is let's delete
all of these by selecting them all
clicking select ultron and then just
hitting backspace and deleting them all
except for one and i'm going to move
this into server script service and in
this i'm just going to change a couple
things so let's change kill brick to
kill bricks folder let's change the
variable to reference the folder so what
i'm getting at here is that we're going
to use a for loop to loop through each
part and make it so whenever that part
is touched it will kill the character so
game.workspace.killpark and then we're
gonna do our for loop so for underscore
comma v in pairs kill bricks folder get
children and then arrow key out of that
do and then let's just uh select all
this hit control x and paste that in
there and
let's just change v we can just change v
to kill bricks underscore will just mean
that it isn't counting like how how many
kill bricks we've already looped through
it just won't save that table it it's
just a little bit better on performance
to do that if you don't need that number
like sometimes you might want to check
which kill brick you're on for example
and if it's on the fifth kill brick
maybe you want it to stop or something
well we don't need that right now so
that's why i did that better for
performance so we changed v to kill
brick and v
v is just the item we're on currently in
the table so right now it'll be the
current kill brick and
replacing that in this script should
work perfectly
and we should delete our little practice
weight there if we have that still and
there we go so
now our kill parts will work and now
maybe we should make some checkpoints so
let's go ahead and make a new
checkpoints folder and rename that to
checkpoints and let's go ahead and add a
part oh yeah before we do anything else
let's make sure they're all anchored and
i'm going to go ahead and scale this
down as you guys can see i'm probably
not the best at building hobbies but
there we go um let's honestly make this
neon and then i'm going to make this
like white yeah there we go i don't want
too much glow on it so i think smokey
grey looks like a great color and next
we'll start scripting this so uh let's
add a new script into server script
service and let's rename it to
checkpoint script checkpoints script and
we're gonna do the same thing as we did
before so local
check checkpoints folder equals
game.workspace.checkpoint and for
underscore comma v in pairs checkpoints
folder get children do if v is a base
part well it checks if it's a part of
any kind
then
v dot touch connect function
and now we need this to do something so
let's define that player real quick so
we're gonna do connect function part
okay we'll do part and then if part dot
aaron here let's go ahead and add the
player servers because we do need to
check if this is a player so local
players equals game get service players
and we'll do it if players get player
from character part dot parent so i'll
check if the model that the part is in
is a player oh actually i keep changing
things but we're gonna cut this select
it hit control x and let's do local
player equals just a little bit better
so we don't have to do it again so if
player
then what we're going to do is we're
going to add a stage to that player and
we're going to check if they haven't
already touched that checkpoint or we're
going to set it to that number of
checkpoint so what we want to do is drag
our checkpoint in here and we're going
to rename this to one so we'll just have
the checkpoint number be uh the name of
the part now we do need to set up a
leaderboard real quick so this will be
really easy so we're gonna do is we're
gonna make a leader stats it's gonna
save what stage they're on while they're
in game it'll reset when they leave but
this will make it so that
when you join the game it's going to
make you a stage number so
local
leader stats oh wait no
gate players dot player
so players
dot
player added
connect function
player
local
leader stats
equals instance dot new
folder uh leader stats dot parent equals
player
local
stage equals instance dot new
int value so let's make a new integer
value which is just a number no decimal
and it will save what stage it's on and
we're going to put that in leader stats
so it'll show on the leaderboard as a
stage and then the number so
stage dot parent equals leader stats and
that's all that's needed so if player if
player and let's make a variable for
their stage so local stage value equals
player.leaderstash.stage
if player and stage value is less than
two number so two number will make this
to a number v dot name so that'll check
so the the name of the checkpoint and
that'll get the number the name of the
checkpoint will turn into a number and
it will check if the player is not on a
stage
that is greater than that so we don't
want them going back stages that's
basically all this does and it'll set
the stage value oh yeah we do need to do
stage value dot value
because stage value is a
object it's the uh in value we have to
do the dot value because it's a property
so stage value dot value equals v name
okay that was a lot if you did not catch
it feel free to rewind and feel free to
copy down pause and copy down what i
have um if you missed it this should set
up our checkpoint in our game so
if we go ahead and play right here oh we
didn't name the folders here we have a
little problem we did not name the
folders
leaderstats.name equals leader stats and
stage
dot name
equals
stage there we go
another error i didn't capitalize that
oops this is the hardest part about
scripting most annoying part
there's a lot of bug fixing and little
annoying things like that like one one
capitalization can screw up your whole
script it gets annoying but
there we go finally it works so
we touch this and now we have we're at
stage one we die
when we touch a kill brick
and
that
is good we do want to make sure that we
move the character to the checkpoint
when they respawn
so if we do
player.character
added connect function
character
or just you char
local
checkpoint
equals
checkpoints folder find first child do
stage.value so if it finds the stage
value
in the checkmates folder it's basically
checking if their current stage even
exists then we're going to do
player dot
primary part and what this does is it'll
get the human android right
the humanoid root part stumbling over my
words humanoid root parts or
the torso if using r6 and it will move
to where we want it so
player
player.primarypart.c frame equals so c
frame is just the way we do positions
and rotations
all in one nice value in roblox studio
it's kind of weird and complex
i mean i don't even completely
understand it's a very complex subject
um c frame but with the c frame we're
just going to go ahead and write
c frame dot new
check point dot c frame dot
rotation
equals dot c frame dot
okay
okay check point dot primary part dot c
frame dot position dot y plus five
so we're adding five studs so they spawn
above it i'm making this way too
complicated aren't i
checkpoints dots
okay let's hope this works
a few moments later and of course it
didn't work
oh okay
part.c frame no that needs to be
player.character
dot c frame i screwed a lot of things up
here i'll be honest with you guys i
don't know how
how to make this respawn right
but i think you guys get the point of
the obby i don't like making hobbies
when in doubt go to the dev hub on
roadblocks it's a great time for me to
do it but you can go to the dev hub so
you search for roblox dev hub
developer.roblox.com
when you go to this you can find info on
everything and when you can you want to
go from here or sometimes when you
google a question uh you can find a
question answered on dev forum or
scripting helpers but usually you're
going to end up back here so you want to
try and read this when you can learn
some stuff about scripting and if you
want more advice on how to get
commissions on roblox and how you can
earn tons of robux from scripting or
whatever you'd like to do make sure to
check out the video on screen do you
guys there
[Music]