00:00:00
welcome back to another roblox studio
00:00:01
tutorial in today's video we're gonna
00:00:04
start a series on first-person shooters
00:00:06
and we're gonna be adding different
00:00:07
things like how to set up a gun we'll
00:00:09
set up leaderboards that keep track of
00:00:11
kills and deaths and then also do a few
00:00:13
more things so one of the things we're
00:00:15
going to take a look at is how to create
00:00:16
a gun so this gun right here can shoot
00:00:18
bullets and then if the bullets hit
00:00:22
another player they'll do damage so let
00:00:24
me go and find where my guy went to he's
00:00:26
trying to run for me okay and if you're
00:00:33
playing on a server with other players
00:00:35
then it'll actually keep track of how
00:00:36
many times you kill a player and also
00:00:38
how many times you die so let me go
00:00:40
ahead and load up a local server and we
00:00:42
can take a look at that part of it okay
00:00:43
so now if you're in a server with some
00:00:45
other players and let's say I kill this
00:00:46
player up top here then in the
00:00:50
leaderboards you can see that my player
00:00:51
two got one kill and then player one has
00:00:54
one for the deaths category alright so
00:00:57
let's go and dive in and see how we can
00:00:58
do some of this stuff in roblox studio
00:01:02
[Music]
00:01:06
all right so in this series we're going
00:01:08
to start off by getting the gun to shoot
00:01:10
bullets to do that we need a gun model
00:01:12
and for the gun model you can either
00:01:14
create your own if you want to do that
00:01:16
for this example I just took this one
00:01:18
from the toolbox and wrote my own
00:01:19
scripts for it so let me go ahead and
00:01:22
show you where I got that from so if you
00:01:24
go under the View tab and open up the
00:01:25
toolbox and then search gun up in the
00:01:29
search bar I use this one right here the
00:01:34
one that says pistol and then for the
00:01:38
- I just cleared it off and deleted all
00:01:40
the extra stuff so basically I deleted
00:01:43
everything except for the actual model
00:01:56
all right so that's the only part of it
00:01:58
that I'm using so I'm not actually using
00:02:00
any pre-made scripts that came with it
00:02:02
so we're actually gonna be writing our
00:02:03
own scripts for this so that you can
00:02:04
take that script and move it to any
00:02:06
model that you want to the one thing
00:02:09
that's important to keep in mind is when
00:02:10
you're searching through the toolbox for
00:02:11
a gun they're not all created the same
00:02:13
so what you're really looking for is a
00:02:16
tool where all the parts are grouped
00:02:17
into one part and then you want to make
00:02:19
sure that part is called handled if you
00:02:22
find a tool that you like and all the
00:02:23
parts are kind of separate see if you
00:02:26
can go under the model tab and select
00:02:29
all the parts and use the Union button
00:02:30
to group them all together and then
00:02:32
rename that Union to handle so the
00:02:35
script that we're going to be doing
00:02:36
should work for most items as long as it
00:02:38
looks something similar to this where
00:02:40
all the parts are composed into
00:02:41
something called handle all right so
00:02:44
while we have the toolbox open let's go
00:02:46
ahead and get a sound for our gun as
00:02:47
well so if you click on the drop-down
00:02:49
menu you want to select audio and then
00:02:53
in the search bar you can search
00:02:54
something like gun shot and then you can
00:03:01
take a look through some of these to see
00:03:02
what they sound like and once you find
00:03:05
one that you like you're going to click
00:03:07
it that'll insert it into the workspace
00:03:10
and then from the workspace you just
00:03:12
want to drag it inside the handle of the
00:03:14
handgun alright so the next thing we're
00:03:18
going to do is start working on a local
00:03:19
script for this tool so go ahead and
00:03:21
find your gun over in the explore menu
00:03:23
and click on the plus sign and then
00:03:25
we're going to be adding a local script
00:03:26
for this local script we're going to
00:03:28
start by making a reference for the gun
00:03:30
so we'll say local gun and this is going
00:03:33
to be equal to script dot P Hrant after
00:03:38
that we're going to make a reference for
00:03:40
the sound so we'll say local and then
00:03:43
we'll say gun underscore shot and this
00:03:46
is going to be equal to gun dot handle
00:03:49
and then inside the handle is where we
00:03:51
stored the sound so it's right here so
00:03:55
since there's a space in between gun in
00:03:57
the word shot we're going to use square
00:03:58
brackets and then we'll put the name of
00:04:00
it inside parentheses so we'll say gun
00:04:03
space and then shot next we're going to
00:04:06
create a function that will run whenever
00:04:08
the
00:04:08
clicks so we're going to start by saying
00:04:10
gun da equipped so this is when the
00:04:14
player equips the gun we're going to say
00:04:16
: connect inside the parenthesis we're
00:04:19
going to define a function inside this
00:04:23
function we're going to pass the mouse
00:04:24
object and then inside this function
00:04:28
we're going to define another function
00:04:29
we're gonna say mouse dot button one
00:04:33
down and then we'll be connecting this
00:04:39
inside the parenthesis will be another
00:04:41
function this time we're not going to
00:04:44
pass any parameters but what we're going
00:04:46
to do first inside this function is just
00:04:48
play the sound and we can do that by
00:04:50
saying gun underscore shot and then
00:04:54
colon play so this will be a good test
00:04:59
to see if whenever I click the mouse if
00:05:01
I can hear that gunshot alright so let's
00:05:03
go and run the code and check it out ok
00:05:06
so now whenever I equip the gun and
00:05:08
click the mouse we hear a gunshot okay
00:05:16
so next sound the to-do list is to get a
00:05:17
bullet to shoot from the gun we're going
00:05:19
to be doing this in a couple different
00:05:20
parts but the basic idea is we're going
00:05:22
to have a click event on the client side
00:05:24
we're going to send some information to
00:05:27
the server side such as where the click
00:05:29
is happening and also the guns position
00:05:31
and then from there we're going to
00:05:33
create a part and use velocity to make
00:05:35
it shoot all right so let's go and go
00:05:37
back to the local script and we'll get
00:05:39
started on that so since we need to
00:05:42
connect the client side to the server
00:05:43
side we're going to be using remote
00:05:44
events so let's go ahead and define
00:05:46
those right here
00:05:47
so first we're gonna say local replicate
00:05:50
it's storage and this is going
00:05:56
equal to game : get service inside the
00:06:00
parentheses is going to be replicated
00:06:01
storage and then so next what we need to
00:06:05
do is actually create that remote event
00:06:07
so under replicated storage we're going
00:06:10
to click on the plus sign and you're
00:06:13
going to add a remote event and then go
00:06:16
ahead and rename that remote event
00:06:18
two-shot event after you do that we're
00:06:22
going to define a variable for that
00:06:24
remote event and we'll say local remote
00:06:27
event and this is going to be equal to
00:06:31
replicate a storage which is where it's
00:06:33
located inside replicated storage we're
00:06:36
going to say : wait for child inside the
00:06:39
parentheses we're going to put the name
00:06:40
of the remote event which is shot event
00:06:47
so next inside this function in addition
00:06:50
to playing that sound we also want to
00:06:52
trigger that remote event so that we can
00:06:54
write a script on the server side to do
00:06:57
that we're going to say remote event :
00:07:00
fire server inside the parenthesis we
00:07:06
can pass some information to the server
00:07:07
and what we're going to give to the
00:07:09
server is the position of the gun which
00:07:11
is going to be gun dot handle dot
00:07:14
position we also want to send the
00:07:18
location of the user's mouse which we
00:07:20
can do by saying mouse dot hit dot P all
00:07:25
right so now that we have everything
00:07:26
handled on the client side let's go and
00:07:28
work on the server side now so we're
00:07:30
going to be inserting a script into the
00:07:31
server script service so go ahead and
00:07:34
click on the plus sign and then click on
00:07:36
script and then you want to rename the
00:07:38
script to what I have right here which
00:07:39
is bullet create inside the script we're
00:07:42
going to start by referencing the
00:07:43
replicated storage and also the remote
00:07:45
event since those are going to be the
00:07:47
same as what we just wrote if we can
00:07:49
head back to the local script and copy
00:07:50
these two lines right here and then
00:07:53
we're going to paste them inside of our
00:07:55
script here next we're going to write a
00:07:56
function that's going to run whenever
00:07:58
the remote events gets triggered which
00:08:00
will happen whenever a user clicks their
00:08:01
mouse so we're gonna say remote event
00:08:04
dot on server event
00:08:09
and then we're going to connect this
00:08:12
with a function inside the function
00:08:16
we're going to pass the information we
00:08:18
got from the client so from the client
00:08:20
side we get the player automatically
00:08:22
which is the player that triggered the
00:08:24
event we also sent two additional pieces
00:08:26
of information so if we look at the
00:08:29
local script we sent the position of the
00:08:30
gun so I'm going to create a variable
00:08:33
called gun and then POS for position we
00:08:37
also sent the mouse position so to store
00:08:39
that I'm going to say mo s and then POS
00:08:42
for position okay just a test to make
00:08:46
sure that this is working let's go ahead
00:08:48
and print off those two different
00:08:49
variables so we're going to print the
00:08:51
guns position and they were also going
00:08:54
to print the mouse position okay so
00:08:59
before we run the game let's go up to
00:09:01
the View tab and select output so that
00:09:03
we can see the result so now I'm going
00:09:05
to run the game so this arrow right here
00:09:07
is part of the NPC so don't worry about
00:09:08
that part of it so now whenever I shoot
00:09:12
the gun you'll see a couple sets of
00:09:14
numbers right down here the first one is
00:09:16
the guns position and the second one is
00:09:20
the mouse position so by doing this we
00:09:27
know that our remote event is working
00:09:28
and we have those two pieces of
00:09:29
information so the next thing we can do
00:09:31
is start working on creating the bullet
00:09:33
all right so let's start working on
00:09:35
creating the bullet whenever the user
00:09:36
clicks the mouse so we want this to
00:09:38
happen on the server side so we're going
00:09:40
to be writing this script under the
00:09:42
bullet create script that we put under a
00:09:43
service trip service so for now we can
00:09:47
go and delete these print messages since
00:09:49
we know what's working so what we're
00:09:51
going to be doing is creating a new part
00:09:53
and then changing some of its properties
00:09:54
to make it look like a bullet for now
00:09:56
though let's just keep it simple and
00:09:57
just create a normal part we can do that
00:10:00
by saying local bullet and this is going
00:10:04
to be equal to instance dot new inside
00:10:07
the parentheses we're going to be
00:10:08
creating a part then we're going to say
00:10:11
bullet dot name and this is going to be
00:10:16
equal to bullet
00:10:20
after that we'll say bullet dot parent
00:10:23
and this is going to be equal to game
00:10:26
dot work space and then we're going to
00:10:30
set the position of the bullet by saying
00:10:32
bullet dot position and for now we're
00:10:36
going to set this equal to the guns
00:10:38
position by saying equal to and then gun
00:10:40
POS we're gonna be changing this as we
00:10:44
go along for now though we're just going
00:10:45
to test to see that whenever the user
00:10:47
clicks the mouse it's going to create a
00:10:49
part we don't really care what the part
00:10:51
looks like at this point we just want to
00:10:52
see that something is created all right
00:10:54
so let's go and run the game and check
00:10:55
it out okay so now whenever the user
00:10:57
clicks the mouse you can see a part
00:10:59
appears obviously this is much too large
00:11:03
for a bullet but it's a good start to
00:11:05
see that we created an object so now
00:11:07
that we have an object being created
00:11:08
whenever the user clicks the mouse the
00:11:10
next thing we're going to work on is
00:11:11
getting this object to move just like a
00:11:13
bullet would we're gonna leave it big
00:11:15
for now just so it's easy to see that
00:11:16
motion and once we have the motion down
00:11:18
then we'll go back and refine the bullet
00:11:20
so we're gonna head back under the
00:11:23
bullet create script and what we're
00:11:26
going to do to make this object move is
00:11:28
start with a distance variable which is
00:11:30
going to be the distance between the
00:11:31
mouse and the gun we're going to define
00:11:33
that by saying local distance is going
00:11:37
to be equal to parenthesis Mouse
00:11:39
position we're gonna say minus gun
00:11:43
position so you can think of this the
00:11:46
mouse is far away so we're taking the
00:11:48
mouse's position and subtracting the gun
00:11:50
position and what that's going to do
00:11:52
it's going to give us a distance between
00:11:53
those two points outside the parentheses
00:11:57
we're going to say dot magnitude so all
00:12:02
together that's going to give us the
00:12:03
distance between the mouse position and
00:12:05
the gun then we're going to define a
00:12:06
speed for our bullet so we'll say local
00:12:09
speed is equal to for the value here I'm
00:12:13
going to do 500 you can adjust this if
00:12:15
you want to to make it the bullet faster
00:12:17
or slower after that we're gonna say
00:12:20
bullet dot C frame so this will be the
00:12:24
position of the ball in the game and
00:12:25
this is going to be equal to C frame dot
00:12:29
new inside the parenthesis is going to
00:12:33
start at the guns position
00:12:34
and go to the mouse position and then
00:12:38
here we're going to be adding the
00:12:39
velocity for the bullet by saying bullet
00:12:41
dot velocity and this is going to be
00:12:45
equal to bullet dot C frame and then
00:12:51
we're going to say dot look vector and
00:12:54
then we're going to multiply this by the
00:12:56
speed all right so let's go and run the
00:12:58
code now and see what we have okay so
00:13:00
now when I click the mouse we have a
00:13:02
part that shoots off all right so
00:13:11
everything looks good so far before we
00:13:13
move on to making the bullet look more
00:13:14
like a bullet let's go in adjust the
00:13:16
speed variable so you can see what
00:13:17
effect that has so back on the script
00:13:20
here let's make it something small like
00:13:22
maybe 50 and now if I shoot my gun let's
00:13:24
take a look at the bullet so you can see
00:13:28
if I lower the speed it shoots much
00:13:30
slower and also doesn't shoot as far and
00:13:34
let's go back to the script now we'll
00:13:35
try something larger than 500 so before
00:13:39
was 500 so let's double that and see
00:13:41
what 1,000 looks like all right so I'm
00:13:43
going to shoot my gun with the speed set
00:13:44
to 1000 so it's similar to the 500 but
00:13:50
it's just going a little bit faster so
00:13:51
you can adjust that number to whatever
00:13:54
you like I think 500 looks pretty good
00:13:56
so that's what I'm going to keep it at
00:13:57
all right so so far we have created the
00:13:59
bullet object and use velocity to make
00:14:01
it move so the last thing we're going to
00:14:03
do before we end with this video is to
00:14:04
make it look more like a bullet so to do
00:14:07
that basically all we're going to be
00:14:08
doing is changing the properties of it
00:14:09
so up here we'll we define the bullet
00:14:11
object let's go ahead and add a couple
00:14:13
more things so first off I don't need
00:14:16
this line of code anymore since we're
00:14:17
using the velocity down here to move the
00:14:19
object so the first thing I want to do
00:14:22
is change this from a block part into a
00:14:25
cylinder and we can do that by saying
00:14:27
bullet dot shape and this is going to be
00:14:31
equal to e num dot part type and then
00:14:36
from the part type we're going to select
00:14:37
cylinder
00:14:38
next we need to make our bullet quite a
00:14:40
bit smaller than it already is and we
00:14:42
can do that by saying bullet dot size
00:14:44
and this is going to be equal to a
00:14:47
vector
00:14:48
dot new so what you're going to put
00:14:51
inside these parentheses will be the
00:14:53
dimensions or the size of the object the
00:14:55
first part will be the X the second part
00:14:57
will be the Y dimension and the third
00:14:59
part will be the Z so after
00:15:01
experimenting some numbers I think look
00:15:02
pretty good as a starting point or 0.5
00:15:05
for the X 0.25 for the Y and 0.5 for the
00:15:12
Z finally let's go ahead and change the
00:15:14
color of it and we can do that by saying
00:15:16
bullet dot brick color is going to be
00:15:19
equal to brick color dot new inside the
00:15:25
parentheses you can choose the color for
00:15:27
now though I'm just gonna choose gold
00:15:29
alright so let's go ahead and our script
00:15:31
now and see how our bullet looks
00:15:33
so now if I shoot my gun it looks a
00:15:35
little bit more like a bullet it looks
00:15:37
like it's sideways though so let's see
00:15:38
if we can fix that real quick so to help
00:15:40
us out a little bit to see which way we
00:15:42
need to rotate the object I'm just going
00:15:43
to insert a cylinder into the game real
00:15:45
quick and then if we use the rotate
00:15:47
option and then for the properties of
00:15:49
this part what I'm going to be looking
00:15:51
at is the orientation so let's go and
00:15:53
use the green ball to rotate it to the
00:15:55
way we want okay and we see for the
00:16:00
orientation I need to change the Y part
00:16:01
to negative 90 and on the script here
00:16:05
the way we're going to change that is by
00:16:06
saying bullet da orientation and this is
00:16:10
going to be equal to vector three dot
00:16:12
new inside the parentheses we're gonna
00:16:15
put 0 for the x part negative 90 for the
00:16:18
Y and then 0 for the Z all right so
00:16:22
let's go ahead and run the code and
00:16:23
check it out okay and now if I shoot the
00:16:25
gun the bullets are facing forward so
00:16:30
feel free to play around with these
00:16:31
numbers to customize the look of the
00:16:33
bullet all right so I think this is a
00:16:34
good stopping point for this video so in
00:16:36
this video what we did is we took a gun
00:16:38
model which you can either create or
00:16:40
take from the toolbox and then we added
00:16:42
a local script to that gun so that
00:16:44
whenever the user clicks the mouse it's
00:16:45
going to play a sound and then send a
00:16:47
few pieces of information to the server
00:16:49
and then on the server side we actually
00:16:51
created that bullet object we changed
00:16:53
some of its properties to make it look a
00:16:55
little bit more like a bullet and then
00:16:56
we change the bullets velocity to make
00:16:58
it move alright so I hope you enjoyed
00:17:00
this video and stay tuned
00:17:02
the next one
00:17:02
[Music]