Uncategorized

Roblox Gun Tutorial – How To Make A Gun


hey guys this is AlvinBLOX and in
this tutorial I’ll be teaching you how to make a gun on roblox like this now
this gun comes with its own equipping and reloading animations it works for
the r15 and r6 body types it is compatible with filtering enabled on or
experimental mode off to prevent hackers in your game
it has custom sounds you can integrate it with a custom kill and wipe out
leaderboard and it also has its own double damage headshot system so if you
shoot somebody in the head they will get killed if you have double damage and
you’ll get this headshot coming across the screen so if you want to learn how
to make this stay tuned let’s get into the video so hello welcome to this
tutorial if you are new to my channel welcome it is great to have you here and
if you want to become a better roblox developer and you want to master roblox
scripting then you’re going to want to make sure that you’ve subscribed to my
youtube channel and you’ve turned on the notifications so you don’t miss anything
firstly we’re going to want to create our tool and design it so we’re going to
want to right-click on the starter pack hover over insert object and we’re going
to find a tool I’m going to call my tool gun by viewing the properties tab by
clicking view properties make sure explorers open too and you’ll be able to
edit the properties of the tool so I’m going to change the name property to gun
and the tool tip to gun as well this is what will show up when the player hovers
over the tool to equip it once we’ve done that we’re going to want to insert
a few things we’re going to firstly want to insert a sound object this will be
the sound which plays when we fire the gun so let’s find sound there it is I’m
going to call this gunfire and then I’m going to right click again on the tool
hover over insert object and I’m going to
inserts a a number value and I’m going to call this value bullets this will be
the amount of bullets that we have have left in our gun so we can see if we need
to reload or not so bullets and then I’m going to insert a local scripts this is
where we will be actually coding the gun to make it shoot so we’ll insert that
local scripts and just leave it for now then we’re going to want to insert
animations we’re going to want to have two animations so right click on the gun
hover over insert objects click on animation and insert another one or you
can just press ctrl + D to duplicate it we’re going to name both of these
animations the first one will be called reload and the second one will be called
shoot once we’ve done this the last thing that we need to do is get our gun
design so you can find your own in the toolbox I’m going to use a 3d model
which I found I’m going to insert it into my game now I’m just going to copy
it out and I’m going to paste it into the workspace and you can see it is this
design I found it on the roblox website you can use your own 3d model if you
want I’ll just find 3d models to look more realistic so I’m going to take this
handle and I’m going to drag it into the gun make sure that is called handle with
a capital H else it will not be welded to the players hand once you’ve done
this you’re going to want to click on plugins and you’re going to want to find
clone trooper ten nine teams tool grip editor plugin now you can find this in
the description of the video and you will need to install it by clicking on
manage plugins in studio then you can click on find plugins go to the link and
install it from roblox studio once you’ve got the plug-in open you’re going
to want to position it so that it fits in the players hand like a normal gun so
I’m going to turn around and used the orange button to make it
fit like that that’s fine I like that so I’m just going to click away and it goes
away in fact I’m just gonna deselect the tool there we go
so that is our tool done we can check out the game if you want and you can
equip the gun and it’s in your hand so that’s the tool design completed
we’re now going to want to set up our gun GUI this will be the GUI which shows
up to tell us how many bullets we’ve got left and if we hit somebody in the head
and get a head shot so we’re going to want to design this first so we’re going
to right click on the starter GUI hover over insert objects and click on screen
GUI click on the GUI we’ll just insert it and I’m going to name it gun GUI just
like that press ENTER and then you’re going to want to right click on the gun
GUI hover over insert objects and click on a text label now a text label is a
GUI element which we can’t click on or interact with it’s just telling us some
text we don’t want the player to change the amount of bullets so we’re just
having a static text label which which can only we can only read so we’re going
to then change the text if you want you don’t have to change it but I’m just
going to change it to say bullets with a colon and then six because my gun is
going to have six bullets / / / reload and then going to click on text scaled
and I’m going to want to position the GUI in the in the bottom middle of our
screen so to position it in the bottom middle what we’re going to do is we’re
going to find the position property and we’re going to change the four values to
not 0.5 to put it in the middle of our screen and then gonna do minus 100 as
the second value so the offset so that it
in a true middle for each device so that’s the horizontal axis done and then
I want it to be at the bottom of our screen
so I’m going to put nine point nine because the top of the screen is zero
and the bottom is one so naught point nine is near the bottom and then I’m
going to want to offset it by minus twenty-five so it is in this position
all the time so if we move our GUI it stays in the middle of the screen when
we resize it once we’ve done that what you’re going to what to do you can
customize a bit more the design but once you’ve done that what you’re going to
want to do is add a local script into this text label so just right click on
it hover over insert object and click on
text label we will deal with this in a moment but for now that’s everything we
need for the bullets so just X out of these scripts and we can name this text
label bullets once we have done our bullets we can right click on the gun
GUI again hover over insert objects and click on text label we’re going to add a
another set table which will show up when we get head shot this time I’m
going to have it tweening into our screens are animated so we’re not going
to bother with position positions just yet we’re going to focus on the design
of it so you can design it yourself but to make it look cooler what I’ve done is
I have made the background transparency equal to one and then I have gone down
here to the text color 3 property and you can change the text to any color you
want but for it to stand out the color I chose was RGB value 255 comma 2 3 8
comma 0 and we’re going to want to click text scaled so that it is the biggest
that it can be on the device and we’re going to want to change the font to
let’s have a look let’s do sci-fi and I’m also going to give it a tech
stroke transparency which gives an outline now once you’ve done this what
you can do is what we need to change the text first to say headshot and now
because we don’t need to bother with it yet we’re going to animate it in a
script we can change the position to a position which is off the screen where
we can’t see it so a good position for this is one point five because remember
zero on the scale would be over here on the left and one would be at the very
far right so it’s anything over one would be off the screen we couldn’t see
it so one point five and then comment zero we don’t need to bother with any
offsetting because it’s already off the screen and then we want it to be in the
middle vertically around here so we’re going to have it in the middle not point
five and then we’re going to offset it by 25 so that it’s in its truest
position for devices and it’s not a little bit to ride or a little bit to
the left it’s in the middle we’re going to click enter and it should go off the
screen and we’re finished with the GUI for now so let’s take this GUI and let’s
put it inside of our gun we will be coming back to this GUI later to code it
because we need to we need to keep it updated with the amount of bullets which
we have left okay so the next thing which we’re going to want to do is we’re
going to want to have a leader board which will document the amount of kills
and wipeouts we have now wipeouts we’re not going to be focusing on too much
today this is going to be a basic leader board and if you want it to save data
you can watch my roblox data store tutorial which can find in the top right
corner of your screen now click on the I and you’ll be able to go there so we’re
going to right click on server script service hover over insert objects
and we’re gonna click on script now the code which were going to write I’m just
going to literally copy and paste it into the scripts because I did a lead of
war tutorial before as well so if you’re not really good with leaderboards you
want to know a little bit more as well as saving you can click on the card in
the top right of the screen I’m just copying pasting this in to save time as
I’ve already done a video on it so let’s paste that in whoops there we
go so it’s basically a player at add event which will run when a new player
into the game adds a kill and wipe out value into their player and then here’s
a comment saying if you have any if you need to save any data you can put it
here so that’s the leaderboard script done I’m going to click on the script
and name it leaderboard and now that we’ve done that all that we need to do
is add some remote events and remote functions and then we should be ready to
go now there’s one more script which we will need to write so we’re going to
close this one down hover over service script service again right click it
sorry and hover over insert objects we’re going to insert another script we
keep all of our service scripts in here because it’s running on the server and
not on the client the client is the player so anything in the starter GUI
starter pack starter player that will run on the client and we want these
scripts to run on the server so that they don’t lag the player as much so
this ripped we will get to it later on but this is going to be used for when we
shoot a bullet for it to be working with filtering enabled or experimental mode
we will actually create the bullet part in this script on the server not from
the client so that there’s nothing dodgy going on and not have a filtering
enabled experimental mode tutorial up soon it might be in the top right of
your screen about now ok so I’m just going to name the scripts main scripts
and then that is all of the scripts in our game we’re going to finish setting
up the gun by adding in some remote events and
remote functions which we’ll be accessing in the script later so there’s
no need to worry about what they do right now we’re just going to add them
in so add in a remote function first right click on replication storage and
hover over insert object and insert a remote function and call this remote
function check body type I need to make sure you’ve spouts this correctly to
check body type with a capital C capital B and capital T you’ll see why later on
then you’re going to want to make a nother remote event so you can just
right click on this one and click duplicate and this one’s going to be
called fetch bullets left so capital F capital B capital L for fetch bullets
and left now they are all off the remote function so you’ll need we’re going to
then create some remote events so right click on replicated storage and then
hover over insert object and find it remote event and we’re going to want to
have one two three four five five remote events so you can duplicate this remote
event five times or four times so there we go we now have five of them and we’re
going to want to name each one the first one will be called equipped animation the second one will be called head shots
the third one will be called reload the fourth one will be called shoot event
and the last one will be called unequip animation so make sure that you spelled
all of these correctly if you need to pause the video now so that you’ve got
all of the spelling correct this is crucial for your gun to work so once
you’ve added in all of these remote events and remote functions in
replicated storage we put them in replicated storage because the server
and can both see these and interact with
them because we want the cert the client to call them and the workspace but the
server to receive them and communicate with each other through the filter
enabled experimental mode block okay now once we
have done this we’ve got all of our events added in we can begin scripting
the gun so you’re going to want to go into the starter pack again and find the
gun and open up the local script and then you’re going to want to delete this
print hello world and once you’ve done that you’re going to want to begin by
defining some variables so variables they store data and we are going to use
them so we can easily access things with a quick name so for example instead of
saying script parent for getting the tool you could just say tool so let’s
say local tool equals script parent so this is how we quite a variable we have
local because it’s only local to the script and we don’t need to use it
anywhere else and it’s a little bit faster I think and then we have name of
the variable so tall equals and then you reference where it is so tall equals
script up parent and let’s add a comment saying what we’re doing so getting at
the tool cool so we’re going to then want to get the player variable because
we’ll be using that so local player equals game hold on get service open
brackets and then in speech marks we say players with capital P and then you can
say what local each player because when we are in local scripts you can say
local player to get the player we’re currently working on so we don’t need to
use any names local player will basically return the player who this
script is running on which is really cool so we can then do any other
comments and say that we are getting the player you don’t have to add comments if
you don’t want to I add them because it’s a lot easier for
people to see what each line is doing next we’re going to want to get the
players Mouse which is on the screen so he can track it and track their
movements so we can say a local mouse equals player : get Mouse open racket
closed bracket so you’re just getting the mouse we’re gonna be using this
later on to track why they clicked and then can put the bullet in that location
we’re going to want to have a variable for the sound object which we created
recently so local sound equals and then we can just say tool Co long wait for
child Open bracket close bracket we’re using wait for child just to make sure
that everything’s loaded and ready to go because we don’t want anything starting
before everything’s ready so inside these brackets in the point in speech
marks we’re going to say gunfire because that’s the name of the sound which we
set and then we’re going to just want we’re going to have a value called torso
just put torso equals and then speech brackets I’ll explain what this to us in
the moment or me get to it and then we’re going to have a value called
reloading so local reloading equals false this is a wool value and I’ll
explain what it does in a minute but what is but basically it will check if
we’re reloading already or not so that so that we can’t spam the reload and get
lots of bullets so variable to check if we are already loading and then for the
torso we can just put a comment comment for just to have it to nothing for now
okay so I’m going to drop a line after that and we’re going to want to get the
context action service and this will allow us to make the gun available to
players on mobile so we can say local context action service with a lowercase
C and capital A and capital S context action service equals game :
get service open bracket close bracket and in speech marks context action
service you can have a capital C here and then come through a and a capital S
I just put lowercase C because that’s how most variables are look like so this
will allow us to cater for mobile players so he playing on an iPad or an
iPhone you’ll be able to shoot because we’ll use the context action service
later on and then we will want to check if their body is our 6 or 15 and we will
have a value which tells us if it’s true or not so if it’s our 6 or 15 but for
now we’re going to set that to nil because we don’t need to use it yet so
let’s have a local body no in fact it’s not going to be local we’ll have body
type in fact yes let’s make it local matter local body type equals nil that’s
new for now but we’ll check whether player just our six or our 15 and then
we’re going to want to have another variables or local difference equals
what and what this is later on when we’re checking where they
shot this will be the difference between the head and where they shopped so how
long the bullet is how far it’s got and we’ll use this to see if they can shoot
behind dummy or in front of them exactly etc so we’ll just comment this same
difference between position of head and mouse and then we are going to want to
get the replicated storage service so local replicates storage equals game
curran get service open bracket close bracket and I’m going
to have or replicate a storage inside of please speech marks and once you’ve done
this nearly we’re nearly finished with our variables we’re going to define our
remote events remote events next so these are the things which we just put
inside replicated storage so you’ve got the remote events and we’ll deal with
the remote functions in a minute I’ll explain more on them in a minute so a
local equip animation with low case E and a capital A so variables we like to
have the first word in lowercase and the second word the first letter in
uppercase so it’s that’s just how it works
equals replicated storage because we we defined as a variable earlier and then
we can just get the animation company at the event sorry around kg storage : wait
for child open bracket close bracket a quick animation so we’re waiting for the
animation to be inside of replicated storage and then once that’s in there we
can carry on the script so let’s copy and paste this five times because we are
going to be having five remote events so one two three four five and we’re going
to want two on each line change equip animation to the name of the remote
event so the next one will be called fetch bullets no headshot you can do it
in any order but I have it set out here and I’ve got a plan so let’s change a
headshot there with lowercase and then inside the brackets we all call it with
an uppercase H because that’s how it that’s how we formatted it in replicated
storage and then the next one we will do is called a reload so reload and then we
can just say replicated storage wait for child and then inside here we’ll have
them reload with a couple R because that’s the proper name of it
when were using variables you don’t have to call it it’s real name we can use
lowercase as well so it’s just a time-saver you don’t have to reference
the whole of it I just like to give them a lowercase name and then over here we
have to give them its real name with the capitals so the next one after that will
be shoot event and so we call it shoot event here and we just change your clip
animation to shoot event with a capital S and the capital E and then the last
one will be unequipped animation so on it quit animation just like that and we
will change the text in here to capital u only quit and then animation will have
a capital A so that’s all of the remote events so we can now drop a line I’m
going to have another comment to separate them from the remote functions okay so we’ve only got two remote
functions and that is check body type and fetch it bullets left so we’re going
to say a local check body type you can see how I’ve done a lowercase C for
check but for body type capital B and a capital T check body type equals
replicated storage curl on wait for a child Open bracket close bracket and
we’re going to put check body type in here with capital C B and C because it’s
the proper name up here and we can just copy this line and paste it down below
and we’re gonna call this one fetch bullets left bullets
there we go and the name will be fetch bullets through every starting letter
with a capital letter in uppercase so that is all of the variables for now
which we have to find we define quite a lot of them there
next we will be finding which body type the player is on so are they are 6 or
are 15 because we all need two animations for each body package so to
check which a body type they’re on we’re going to want to call one of our remote
functions now a remote function is or basically when you’ve got a game with
experimental mode off or filter enabled on you are blocked a player so a client
is blocked from talking and editing things on what not talking editing
things on the server so if I had a brick in the workspace a player could not edit
that part so it couldn’t move around or change the color well it could but only
that player would see it and not the rest of the game because the the server
has blocked that change from happening on other players so you can’t change
what would the players see really but when we’re shooting a gun we want to
change what other players see because we are we are we have a bullet flying we
want to see other players get damaged and get hurt and hit so we’re going to
need to access these things on the server and in this case finding the body
type we need to check the player we need to check the players in the workspace we
need to check that they’ve got a ar-15 torso or in our sex torso so in order to
check this the player which because this this the script is running in a player
it will be blocked from looking at that so we need to have access to the server
to to look at this so we use a remote function to to get this for us now it’s
kinda like a middleman in a deal it lives in the replicated storage where
the server and the client can both see and edit things so a player could
call this remote function okay and the server is sat here watching over this
remote function waiting for somebody to call it on on in the game so waiting for
a player or script to call it and when someone calls it the server thinks okay
right somebody has called this remote function and the difference between
remote events or remote functions is remote events will do something when you
call them so we could have one remote event which makes the base plate go
green and call it from a player he would make the base plate go green and then
that’s it but with remote function when we call it from a player the server sees
this it’s been called and it will make the change and then it will send
something back to us a bit of feedback about it could be anything so in this
case we are wanting to check the body type so the server will see this it will
check which body type they’re on and then it will return to us the body type
and this will work with philtrum enabled and experimental mode off so roblox are
making games that are not filtering enabled so which are experimental mode
not available for under-13 so you’re going to want to make sure that this gun
is compatible with it so we use these remote functions to safely and non
maliciously access the server make some changes and grab feedback so we’re going
to call this body type checker I’m going to call it from the player and then the
workspace we’ll see it later on and we’ll script it so that the what the
server is is looking over looking over this remote function and once it’s been
called by us the workspace will check if the player is our six or our 15 and it
will send back to the player the answer so and then the body type variable will
change to either our six or our 15 so we’re going to create a function which
will tell us if the player is our six or our 15 by calling this
event so going to drop line I’m going to add a comment to say fine funky type
just so that we know you’re doing and I’m going to create a function so what
this means is what this means is that it won’t run straight away because we don’t
need to use it yet I’m just defining to the to the script what I want to do when
I want to at different time so I’m getting it ready for when we need to use
it so we’re going to say function find a body type with a lowercase F and an
uppercase B and T for body and type eat not equals we’re just going to have a
pair of brackets after that and then drop a line and you should have this end
added in by roblox and so anything inside this function will not run until
we’ve called it so it will just ignore it
but it will see it it will recognize it and you’ll see a case of function and it
will only run when we say find body type with a pair of brackets after it if
that’s not there it will not run and then it will only run when it sees that
fine body type so we can define everything here ready form we need to
use it so let’s add a comment after this function so you can just say what we’re
doing use to determine whether a player is r6 or r15 so inside of here we’re
going to get the body type ferret variable which we made earlier and we
can say body type equals check body type which is that remote function which we
defined here so it’s the remote function in workspace we’re just referencing it
as a variable and then we can we can call it we can tell the server to do
what we want it to do so find the body type by saying : invoke server open
bracket closed bracket and then in here we’re going to put tool now what this is
doing is it is finding that remote function and it’s
telling the server to wake up we’ve called this function now find what body
type we’re on and then we’re just giving it a parameter of tool and I’ll get to
why we use that later on in the video okay so now that we have called this
we’ve called the remote function or I haven’t done it yet we’ve just we’ve
written out in our script and it will call it when the function has been
called we’re going to just print out the body type into the output not it won’t
make any difference it just allows us to see whether they are our six or our 15
and it just tells us that everything’s worked fine and it’s managed to
successfully get the body type so I’m just going to write that inside print
wrap it inside of there and that is our function completed I’m just going to add
some more comments to just say what we did so we invoke we are invoking
invoking the remote function so you do a check on the server so it’s checking
whether it’s our six or fifteen it’s going to then send us back that body
type because we defined as a variable and then we’re just printing it so it’ll
be either our six or our 15 and we’ll program it to send that piece of
information back later on so now that we’ve done that we can actually get to working with the gun when it’s been
equipped unequipped etc so the fun stuff because we can actually see things
happening so let’s then drop a line and we can add another comment if you want
so when the tool is equipped the following event will run so we’re going
to have an event if you need to have a better understanding one of events for
tools you can watch the video which I did recently that will be in the card in
the top right of your screen now so really good tutorial whoo
tonight use these events but we’re going to be using the equipped event for now
and the code which you put inside of that event will run when the tool is
equipped so we’re going to say tool dropped I quit
turn on connect where the capital C Open bracket function Open bracket closed
bracket and then inside those brackets we can just write Mouse drop a line and
make sure that an end has been added in for you by roblox now we are giving it
the parameter of Mouse because we defined it earlier player get Mouse and
this will tell us at the position where the mouse is located and so when we
equip this we will be accessing the mouse to see where they shot if they’re
if the position where they’re trying to shoot is not behind them so in front of
them or to the side so they just legitimate position so we’ll be needing
the mouse and we are going to want to put all of our code for this inside of
this event so it only works when it has been equipped so let’s get started by
we’re going to want to firstly net will note they’ve equipped the tool we want
to put the gun to UI into their player gyy they can see everything because at
the moment you won’t be able to see it when it’s in the gun only when it’s in
this started GUI but the start of DUI is actually called the player GUI when it’s
inside of a player because anything inside of the starter GUI it gets
replicated into the player to you right and same with starter pack any tools in
so starter pack go into the players backpack so we’re going to in fact let’s
add another variable and we’ll say local burn GUI equals tool curl on wait for
child open bracket close bracket and then in speech marks we can say gun GUI
just that we don’t need to keep saying tool dot gun GUI or script parent dots
gun GUI just say DUI so glen GUI dot codon hello
open bracket close bracket so we’re just cloning the GUI to put it into their
player GUI so then we can say dot parent equals player because we defined the
player earlier dot player GUI now the player GUI is automatically added into
the player we’re just putting this gun GUI inside them then we’re going to want
to find which body type they’re on now we are going to call the function so we
can actually find if there are six or r15 so we can just say find body type
press ENTER and it will add these brackets onto them so very very useful
functions we can call them to run only where we want them to which is good and
now that we’ve done that we want the equipping animation to run so we need to
know whether there are six or r15 and we’ll be doing that later and we’ll be
animating two animations one for r15 one for r6 which will run when they equip
this animation so for now because we’ll be doing that on the server we’re just
going to say a quick animation code on fire server open bracket close bracket
tall dot shoot now what this is doing is it is right I’ll tell you all in a
moment but because it’s a remote event and remote events only make changes onto
the server they don’t send anything back it will tell the server that we want to
equip this animation want to play it on the player and we are sending a
parameter in the fire server event of the animation we’re just sending this
animation with it and it’ll have the animation ID and the server will have
knowledge of the ID which animation want to add into their player and we want to
play it so we use fire server to tell the server that the client one of our
players wants them to do something that’s what remote events do so let’s
just add some comments for the lines which we just made let’s do the cloning
one here there we go okay so I’m going to add a comment I’m just saying we are
cloning new gun GUI into the players veo tu white and then this one here find
body type pulling the function above to check the body type so it will return us
with whether there are six or are 15 and then finally we can say calling the
quick animation remote event so that the server can play the animation okay so we
have successfully created the first three lines inside of this event but
there are lots more to go we’re going to want to add a custom mouse cursor it
makes it easy for the players to shoot each other and makes the shooting more
accurate and to do this we are going to want to get the mouse and we define that
as a variable so drop a line and say mouse icon equals and then in speech
marks you’re going to want to get a decal of a mouse class so now I have a
mouse cursor decal which I made and I uploaded it to roblox you are free to
use it I am fine with that I will leave a link to that in the description below
but the asset ID is 936 803 874 so you’ll be able to find that in the
description and you’ll be able to copy that in yourself but what you want to
say is RBX assets ID code on slush slush in fact no no no you can just copy it
copy in paste it in like that with the asset ID
and it should be fine but if you want to use your own custom one you’d have to
put your ID instead of this one here so you can’t find the mouse icon in the
description down below I’ll put that there for you so that you
don’t have to make your own but you’re more than welcome to make your own and
what we’re going to want to do once we’ve done that is we want to check to
see when the player house clicks their mouse so you can fire a bullet so what
we’re going to do is once again get the mouse and then we’re going to say
mouse dot I’ll turn the one down code on connect open bracket function open
bracket close bracket so bottom one down is an event by roblox which will I will
run this ad necklaces them at an end by pressing enter so any code inside of
this event will run when we press down the left button on our mouse and it will
only work when we have the toured equips because it’s nested inside of this
equipped event so it wouldn’t be there if we didn’t have our tool equipped but
if it was it would be so once we have got the event up and running we’re going
to want to do a check to see if we have enough bullets so let’s say if tool dot
bullet let’s let’s in fact make another variable up here just after the gun GUI
and say local well let’s equals tool current weight full trial open bracket
close bracket and then we can put bullets in speech marks just so that we
have the bullets value ready to go so we can say if bullets dot value is smaller
than or equal to zero or reloading equals equals true then drop line and
make sure that this end has been added in so if we don’t have enough bullets or
we’re already reloading because we said that this reloading value would only be
equal to true and we’re already reloading to prevent spamming we don’t
want to do anything so let’s add a comment saying don’t do anything but if
these things are not happening if they have enough bullets or they are not
reloading then we will be able to shoot so you can just put else and the code to
shoot a bullet will go in here instead because if they’ve got my boots or
they’re reloading it just won’t work but if they’re not else we can shoot our gun
here so the code which we’re going to put in
here when they shoot the bullet we’re going to want to get the positions of
their head and the mouse I’m going on to find the difference between them and
then we’re going to use Ray casting to create a ray which will show the trail
of our bullet so to do this we’re going to set head local head actually local
head equals game got workspace and then in a pair of square brackets like that
we’re going to write player good name and then outside of these brackets I’ll
explain in the moment and I say drop head and then we’re going to say dot C
frame dot look vector now what this is is we
have got we don’t know the name of the player yet obviously but we’ve got it as
a variable here so when the game runs we walk this game dot players this game
gets service players to local player will basically be substituted it will
still be called local player but behind it will be the actual players name so if
I joined the game it would be Alvin blocks but in the script
its disguised as a local player for now because we don’t know which player will
enter the game so when we call this bit of code when this line of code executes
it will go to game dot workspace and we will swap out player name for Alvin
blocks because the variable game dot players that a local player will be
changed to my name and then it will find my head it will find its C frame value
the position but then it will find the look vector so the direction which is
looking in and if you already knew the player’s name you wouldn’t have to use
these square brackets but if we didn’t use them and we just said game dot works
place dot player what name it would get quite confusing for the script because
it would be looking for an object in there called player then it would be
looking for it to Nate the net the name property of that player and then it
would get confused because it would think that there’s a head inside of that
name property which there isn’t so it would cause an error so you have to put
a pair of square brackets in there so you don’t have a dot between workspace
and player you just have a square bracket let’s just zoom in a little bit
on that there we go so you don’t have a dot you only have a dot after it when
you want to find something inside of it so that is the that’s what we using the
head for and we want to get the mouse position we’re going to drop a line and
say local mouse and I know we’ve already got the mouse variable up here but this
mouse variable is only local to this event to the if statement so it doesn’t
cause any distress so we’re going to say equals I’m going to say C frame got you
Open bracket close bracket and then we’re going to say game got workspace
and then in square brackets again and I say player
dot name and then we’re going to get their head and then we want to get their
position of that other head not the C frame for the first value and then the
second value will be the mouse position so where it clicked the actual position
in the game world so mouse dot hit dot P now this isn’t
this mouse here isn’t the mouse here it’s the mouse which we first defined
because we’re still defining this one here now it may get a bit confusing so
do bear with me because I’m will explain it and then we’re going to say dot look
vector so we’re combining these two values and we’re getting the direction
which it’s facing in right and then we’ve got these two positions and then
we can find the difference between them so we can say because we defined our
difference variable up here we’re going to say difference equals and then we can
use that in brackets head – mouse so we’re taking the head look vector
position and then we’re taking the mouse look vector position and we are
subtracting them to find the difference between them and what this will do is it
will allow us to check whether the player is shooting backwards to the left
to the right or in front because we don’t want them firing backwards because
that doesn’t work in real life so all to prevent them from doing that and by
finding the difference between these things it allows us to find the
direction in which they’re shooting now I did some testing before I made the
tutorial and I found that a difference magnitude of which is smaller than one
point three three would not be shooting backwards so if we say if difference not
magnitude it’s smaller than 1.33 then drop a line
and we’re gonna if it is then we know that they are shooting in a legitimate
direction if it’s not smaller than that number then we know they’re not there
there are those shooting backwards something that is not physically
possible I’m not realistic so it’s a good way of
finding out the location which they shot him and I think it makes the gun more
realistic in my opinion around go too much into magnitude but it’s basically
the difference between two two points and the teacher would be very long if I
had to explain it but if they are shooting in the right direction
we can get the remote event which we called shoot event so replicate storage
dot shoot event in fact we would define as a variable earlier I can just say
shoots events there we go shoot event : fire server open bracket close bracket
now because it’s a remote event we are telling the server to do something we
don’t want anything back because it’s not a remote function so we say fire
server not invoke server and then the two parameters which I’m going to give
for this this event are the tool and the position so we’ll get to position in the
minute because it’s underlined in blue but we will be making the Ray in a
minute the position and then the part which will also get to in a moment so
once we’ve done that we’re going to want to subtract one from the amount of
bullets left because that once we get to zero on the bullets value then we will
have to reload because we’re doing the check up here to see if there’s enough
bullets left so we can say well it’s not value because bullets
so let’s dot value minus one okay that’s everything we need to do for making the
bullet fire so let’s get on to making the ray now we could do this on the
server but I decided to do it on the player so we’re going to just want to in
between the difference variable and the if statement here we’re going to create
a ray now array array is a line which goes from one point to another to
another direction infinitely so it won’t stop until it hits something but we only
want our gun to go a certain amount of studs before it hits something because
the bullet will ruin our speed the speed you will start to decrease and it it
will it won’t hit anything so to create our array we’re going to want to say a
local rate which is Ray dot new open bracket close bracket and inside of this
we are going to want to specify the two points where it’s going to go from so we
want it to go from the gun so we’re gonna say tool dots handle dot C frame
dot P this is the position of the handle and then the second point is we’re going
to get the mouse where it where we click the mouse and then we’re going to want
we want to subtract the tools position from that to find the difference between
it and then we will multiply that by in the direction which it is going in by a
certain number of studs so you can have it going off for 300 studs and then it
will stop it will stop going it will just stop in its tracks it won’t go any
further because if you didn’t do this it would go infinitely and just keep going
until it hits something off into the sky so to do this we’re going to
have another paragraph it’s because you want this script to do this bit of maths
first before it works out anything else so we’re going to want to say Mouse dots
in fact we’re going because we’ve got Mouse already defined we’re just this
time we’re just gonna say a player : get Mouse open bracket close bracket dot hit
dot P – tool lots handle dr. C frame dot P not the most efficient I know but it
works for now and then outside of the first bracket but still in the second
bracket we’re going to want to say dot unit so that’s the direction x and then
how many stood you want so I’m going to say 300 because I want you to go for 300
studs if it doesn’t hit anything it will eventually just drop off and it won’t
hear anything and by then it will have deleted okay so now that we’ve got our
array the Ray will keep going and if it hits something it still won’t do
anything so we need to detect when it hits something and make it and we need
to get this part which it hits the first part that it hits so we’re going to want
to define two variables the parts and the positions of the part which it hits
and the position so local part comma position this is useful to fight for
defining two variables at once equals game I’ve got work space : find part on
ray open bracket close bracket you need a capital FP 0 and R for each word fine
Python rate and then inside of these brackets we’re going to say at the array
which we just created in a variable comma and then we can find the player’s
character player dot character and then the last two values the first one is
going to be if it ignores terrain so if it hits terrain in your
game we’ll ignore it no because it will it will stop the rain from going any
further because it’s hit something and then the last one we set to true because
if it hits water then it might there might be somebody swimming in the water
and we want it to go under water so those four values will help us when we
call we’ll find part Andre it will find us help us to find the thing which it
hit now once we’ve done that we can drop another line and we can play the gunshot
sound so we can just say and so what do we define it I think we define it as
sound yes so sound : play open bracket close bracket there we go then so we
have created a tool equipped event which will run when the player has equip the
tool we have created a body type checker which checks if there are 15 or not and
we have made solid progress on the ray casting and making the bullets for the
gun now the tutorial is approaching its first hour so this is the first part
completed and we’ll be moving on to the second part now so now’s the time to
take a break and like the video if you’re enjoying it and you’re finding it
useful all right so we’ve got it working four players on the PC but what about
mobile players well that’s a good question and we all be using context
action service to allow with the player to reload their gun because at the
moment they don’t have a keyboard well they won’t have a keyboard so they won’t
be able to press R to reload so later on we’ll be making a function which allows
the players to reload by pressing the are key but we’re going to have to use
the context action service to create a mobile button on the screen which they
can click to load now I did a video on mobile buttons
so if you want to see that you can click on the card in the top right of your
screen I that was a short too tall I did
explaining how to use them but today we will be going over that in a bit more
detail so we haven’t created our reload function yet but we’re going to use the
we’re going to make the button now just so that we are ready to do our reloading
later on so look at the we’re going to look at the tool equipped event and look
at this final end here so in between these two ends drop a line and we’re
going to want to create our mobile button so we’re gonna say local reload
mobile written with a capital m and capital B where a lowercase R because
that’s how we do variables remember equals so we’re gonna get the context
action service which we defined earlier actually not familiar with context
action service it’s what roblox uses to make games compatible with mobile
devices so allows you to create mobile buttons and to check what device a
player is on so we’re gonna say equals context action service code on bind
action open bracket close bracket and inside here we’re going to have the name
of our button so which we’re just gonna call a reload button so reload BTN you
can call it anything but it’s good to stick with what I’m calling it the
second value which we’re going to have is the function which will make
reloading happen now we haven’t defined it yet but it will be called reload so
type in reload add another comma and we’re going to have true and the last
value will be our in a pair of speech marks now this is the button
which we will press which will allow us to reload the the gun now this one here
is asking us if we want a button to show up on the mobile player screen which we
do so we do want it to show up so we’re going to drop a line and then we’re
going to say contacts Action Service we want to set the position of the button
because at the moment it’s in an awkward position I’m going to say : set position
open bracket close bracket and then we’re going to have the name of our
button in speech marks so the reload BTN which we defined in the line above comma
and we’re going to have the position on the screen what we want is button to go
so I’ve already got the position for you and the position is you dim to don’t
need open bracket close bracket not point seven two this is the horizontal
value which is about three-quarters of the way down the screen and then we’re
going to offset it with minus 25 comma and then the vertical value will be
nought point two comma minus 25 so I’m just going to put not point two zero
which makes it a bit easier for you to read so like I said earlier imagine that
the top of the screen is 0 and the bottom of the screen is 1 and it’s on a
scale so not 0.72 would be quite far down the screen and not point to Oh
would be quite far up so that is the correct value you need I’ve tested it on
mobile devices and it’s in a good position however if you do want to
change it you can mess around with it later on you’re going to drop line and
we’re going to set the image for reloading so we’re gonna say context
option service code on set image open bracket close bracket and then we’re
going to have the name of our button again reload BTN and we’re gonna have a
comma outside these speech marks and then in another pair of speech marks
we’re going to have the asset ID now I’ve got an asset uploaded to roblox
which is a reloading button I’ll show it to you now here is the image I took it
from odds lot he’s got a load of decal images letters which can take for free
but if you want to change it you can do so I just selected this one because it
was appropriate you could create your own which had reloading or something so
yeah you can use yours but if you want to use Rome you will need to copy the
decal ID and then you will need to paste it into studio so you’ll need to paste
it into the link here you’ll need to paste it instead of this ID and I will
leave a link to that asset in the description of the video for you the
image for the reloading button you can find that in the description ok so now we’re going to want to create
our reload event and this will run when we press the our key which we’ve bind it
up to the context action service we don’t need to do any more code because
we’ve told it to run the reload function so we just need to make the function so
we’re going to scroll up to around about line 26 I’m going to drop a line just
after the find body type and I’m going to add another comments just to say
reloading function and inside of here we are going to put the function in so
function func trim eat reload open bracket close bracket drop a line and
you see this end has been added in so any code inside of here will only work
when we are reloading so we want to set the reloading value to true so reloading
dot so you will just say reloading equals true as it’s a variable not a
value and drop line and we’re going to want to fire fire server going to fire
the remote event called reload to tell the server that we want to play the
reloading animation on our player so we can say replicated storage dot reload no
young because I’ve got the remote event up here I keep forgetting that
reload code on fire server Open bracket close bracket and
it’s a tool dot lilo so we’re firing the event and the server will see this and
we’re also sending the animation with it so that the server knows which animation
we want to plug into the player and play and once you’ve done this we want to
update the mouse cursor once again to say that we are reloading I have got
another asset for you to take that will be in the description as well this is
what it looks like there we are so it says reloading
it will show up when you press the a button you can take it by getting the
link in the description and putting it where I put it
or you can just copy it so I’m going to drop a line and I’m going to say mouse
dot icon equals and then speech marks I’m going to copy the item ID and asset
URL I’m just going to paste it in there and then going to want to update the gun
GUI that bullets text label which we made I’m going to want to update that
and are going to want it to say reloading so we will say we’re gonna
drop a line in the reload function and say layer hold on find first child in
facts yes we’re going to do that play up hold on fine first child open
bracket close bracket and we can say play up GUI and we can say hold on
if I’ll say that now I would say player player GUI code on weight or child open
bracket close brackets and we will say gun GUI
make sure that player GUI has capital G and a lowercase UI we’re going to wait
for the gun GUI because remember this function is being called before we clone
it into the player so we don’t want any errors cropping up
so just to be 100% sure we can put weight for child gun GUI because that’s
what we called it but GUI here for gun GUI you can have capital GUI as that’s
what it’s named we can say dots bullets dot text equals and you can just set the
text to reload in in in an industry in speech marks we’re then going to want to
have a wait wait open brackets and we’re going to write in here reload
so we’re going to want to then define another variable so go to the top of
your script and after while we defined the bullets we’re going to add another
variable so in local reload time equals and then put a number so I want there to
be a three-second reload time so just put three if you wanted five seconds you
would write five or six seconds six but I’m going to keep it at three so wait
three seconds and then we want to replenish the stocks of our bullets so
you can say well it’s dot value equals six and then we will change the GUI
bullets text again so we can copy this line here line 33 and paste it here on
line 36 just after the bullets valued and we can change the text back to its
original bullets with a colon then a space then outside of the string we’re
going to use some I think they’re string manipulation and we’re going to say dot
dot we say bullets dot value so this will take the current amount of bullets
we have and add it on to this string so now that we’ve done that we can reset
the mouse icon so we can copy this line of code from earlier and just paste it
in again but we need to change the asset ID to the original mouse icon which is
down here so just copy that and paste it over here once you’ve done that we’re
going to then want to fire the equip animation event again so you can say
quick animation around fire server open bracket close bracket tool dot shoot as
the parameter because we’re sending the animation that we want to play once
we’ve done that we can change reloading to Fox by saying reloading equals false
so that the scripts know that reloading is finished and we’re
allowed to shoot again so that is the reloading part finished there’s only two
more events for the local script of the tool to write and then we can move on to
scripting the server-side scripts which can make the bullet and the damaging and
the head shots etc so we’re going to scroll down and after the tool dots
equips event at the bottom we’re going to have a tool dots unequipped
event so can save tool tops unequipped code on connect open bracket function
open bracket close bracket no prizes for guessing what this event does and when
it runs and then inside of here we’re going to have more code which will
change the mouse icon back to its default because we haven’t got the tool
anymore we have unequipped it we all then get
rid of the equip animation we’ll get rid of the gun GUI and we will take away the
mobile button so we’re gonna say mouse icon equals and then we’ll go have a
pair of speech marks nothing inside that will just default it then we can drop a
line and we can play our unequip animation which will just default the
player back to normal and we will I will be going over the unequip animation when
we get into these service crypts so don’t worry if you haven’t seen it yet
so you can say only equip animation turn on file server open bracket close
bracket and then we’re going to pass the animation which is tall not shoot the
shooting animation which is the one which we ran when we equipped it we’re
going to stop it but we need the animation reference first so we’re
passing it through as a parameter then we’re going to want to destroy the GUI
so you can say pelea drop a line as a
player quiet GUI dot gun gy : destroy open bracket close bracket and then we
can get rid of our reload button by saying context action service : unbind
action with card through mute and a capital A Open bracket close bracket and
then in speech marks reload bTW because that’s the name which we gave it now
there is one more event which we will get to but we need to do some coding on
the server first in order for you to understand it and for the local script
to understand it so for now that is all of the scripting done for the local
script one last thing which I forgot to do you need to open up the gun GUI which
is in the tool click on the text label and we’re going to name it to headshots
just so that we don’t get confused and when we reference it in the script we
will know what we’re dealing with okay so now you can close the gun and you can
close all of the scripts inside and now we’re back at the base plate okay so now
we’ll get onto the scripting of the main server script which will allow us to
handle the animations and make the bullets appear in in the workspace so
we’re going to get started by clicking on the main script in server script
service and we’re going to get rid of this print hello world and we’re going
to want to define a few variables so let’s define firstly the server storage
so we can say local server storage equals game : get service open bracket
close bracket and then inside of these brackets in speech marks we can write
server storage once we’ve done that we can drop a line
and we can also define the replicated storage so you can copy this line and
paste it down below but we can change server storage to replicate the storage
and just copy it again here drop with a proper spelling with capital R and a
capital S notice how the R and the S is lowercase on the variable name because
the first word is always lowercase well in that is the rule for variables
although if you have them normal like that it wouldn’t matter but it wouldn’t
look the same as mine so the we’re going to do two more values and these will be
the names of the kills and a wipe out values so if yours of course something
different called knock out or yours are called
deaths or something like that not kills or wipeouts
you’re going to want to specify them here but if you’re following the
leaderboard which we created at the start of the video you’ll just want to
have kills and wipeouts if you haven’t changed them so we’re gonna say local ko
value with a capital K o and V equals kills exactly the same as it is spelt in
the leaderboard with the capital K and we can do the same thing like we did
with the first two variables we can copy and paste them on the next line but
we’re gonna change ko value to W value for wipe out value and kills will become
wipeouts so you did call your two values something different make sure that you
change them in the script okay so we’re going to firstly create a event which
will wait for us to call the shoot event because remember now we’re dealing with
the server not the player so we’re waiting for the player to call one of
these events or functions and in the server script we use events to to add to
it to sit over the these functions and events to these
remote functions remote events and we wait for them to be triggered by a
player and when they are we can execute some code so we’re going to drop the
line and I’m not going to comment out this this script like I did in the last
one because you’ve got a pretty good understanding of how everything works
now by looking at the local script I’m just going to go over it and it will
make the tutorial quicker as well so we’re going to firstly write practically
to storage because that’s where the events are situated and we’re going to
get started with the shoot events now this will run when somebody shoots their
gun and when they click their mouse we called it in the in the local script and
we’re on we are working on the receiving and now so replicate storage dot shoot
event reference in the event dot on server event :
Connect Open bracket function Open bracket close bracket and then drop a
line and the parameters which we’re going to put in here are the ones which
we sent so we sent when we did the local script and we fired this let’s just do a
find and it’s events fire server this is where we called it and we gave it three
parameters to position and part now when we’re working with remote events roblox
automatically passes through the player who called this event as well so we’ve
actually got an extra parameter here so if we go back to our service ripped we
need to put all of these parameters in first arguments so the first one is the
player which roblox automatically sent and that has to go first because it’s a
default roblox set one and we and that’s just the order it goes in so player and
then the other ones were cool position and part it’s good to keep them in order
as well so that they are so that you are dealing with the correct one next we are
going to now that we’ve got our event set up this will only run when the event
has been triggered by the way so it won’t run straight away we’re going to
want to have an if statement which will check if the player is alive
so if game don’t work space and we need to get the players name again so we’re
going to use square brackets and type player dot name and we’re going to close
the square in bracket and sage humanoid dot health is smaller than or equal to
zero so it’s either zero which is dead or a minus number a negative number
which means they’re also dead then they’re going to be dead so the
player is that do not do anything and it won’t run until it’s called again and
the player is alive so we’re then going to have an else so
if they are alive then this is where we’re going to do most of our coding so
firstly we want to have a variable which is the amount of damage which a normal
hit will do so last let’s add that to the top of our script local damage
equals 30 I’m taking away 30 of the health every time they get shot which i
think is a reasonable number if you think about how many shots the player
has to do that’s four shots and they could get a head shot as well which
we’ll work on so we need to have a variable of the distance again the
distance of how far the the ray is traveling so we’re going to say local
distance equals and in brackets going to say tool dot handle dot C frame dot P
like we did earlier – position remember we we had the position as the parameter
which we sent from the local script and we defined that where did we define it
here we defined it here so when we did the fine part on Ray that’s where we
defined it and we’re taking it away from the the handles position so it tells us
the distance of how far the Ray is going to travel and then we’re going to say
don’t magnitude on the end of that which is the length we’re then going to drop a
line now that we’ve got our distance we can check if the player has already shot
the gun before and they’ve got a bullet trail already in the game which we want
to delete because we don’t want them to spam the
gun and have lots of annoying bullet trails still in the game
so because we’re going to call these bullet trails later on we’re going to
give them a name called their trajectory so if your name was Alan blocks the line
shot from your gun would be called Alvin blocks his trajectory so we’re going to
say if game dot workspace prolong find first child and then in brackets we’re
going to say flare dot name dot dot and then we’re going to have a pair of
speech marks and inside of those we’re going to have an apostrophe s space
trajectory with a capital T you have to get this correct this is what it looks
like I’m gonna zoom in you have to have it exactly like this because this is
what the part is going to be called so if your name was Ivan blocks I would say
Alvin block says trajectory and it’s merging the players name with the rest
of this string we’re using string manipulation here and the two dots
allows us to add it to this string so let’s zoom back out again there we go
so if there if we find a trajectory already there so a bullet path then
we’re going to want to delete it so we can say game dot workspace
codon fine first child in case it somehow got deleted for some reason we
can wrap it in a fine first child so it doesn’t break the script player not
named top top and the same thing again a pair of speech marks within apostrophe s
trajectory and then we can say : destroy this will get rid of it and we
can create a new one so let’s create another bullet path so I’m going to call
it a trajectory because it’s the diet it’s the path of the bullet the
direction it’s going in so local trajectory equals instance dot new open
bracket close bracket I’m creating a part in the workspace which will go in
the direction of my bullet so it’ll be like a bullet path there’s no actual
bullet this will be this will be instead of the bullet but it will look like the
Buddha has flew when you see this bullet trail so a bit of an allusion to the
player so we’re going to insert apart then outside of these speech marks we’re
going to put a comma and then game workspace because this is a really quick
way to tell the script where you want it to be placed so I want it in the
workspace so I can just have a comma after what you’re inserting and then I
can write game dot workspace which is pretty cool because it saves you a line
of code so now I’m going to insert some smoke into this trajectory so I’m going
to say so bit like a smoke when you shoot the gun so I’m going to say a
local smoke equals some storage make sure you have the capital S for the
service storage variable and then I’m going to say dot smoke particle coal on
Chlo I can bracket close bracket now we need
to make this smoke particle which we’ll do in a moment and then we can say smoke
got parent equals tool doctor handle now remember because we called this from the
tool we gave a parameter off the tool so you know which tool this is going to be
inserted in now because we have filtering enabled on if we didn’t use
the the event the remote event we wouldn’t be able to do this so this is a
cool thing about remote events you can pass anything you want in the parameter
and argument section so let’s create our smoke
I have created it already so what you’re going to want to do is go to server
storage and you’re going to want to find a particle emitter so right click on
server storage hover over insert object and click on particle limiter and then
you can edit its properties to look to make it look how you want how you want
it to look but I’ve already got my particle and you’ll be able to take this
later on so I’m gonna paste it into cell storage and you see I’ve edited the
color the size it gets a bit bigger and then it goes smaller the transparency as
well I’ve changed and the texture of the particle I’ve changed also and a few
emission properties as well so you could make your own but I am providing one for
you as well so back into the main script we can continue by changing some
properties of the trajectory so that it looks how we want it to look so going to
drop a line and say trajectory dot brick color equals brick color don’t you open
bracket close bracket and so inside of here I’m going to specify the color of
the smoke which will be institutional white you can choose your own color from
the color palette but make sure that the brick color is spelled correctly so
institution all whites with a lowercase W for the
white once we’ve done that we can change the material so use a trajectory dots
material equals and then in speech marks we can say smooth plastic so that we
don’t see any studs on it or anything it or a certain texture it just looks like
a natural or literal then we can change its name because you want it to be
called the player’s trajectory so trajectory got name equals player dot
name dot dot and then speech marks we have our apostrophe s and then
trajectory so we’re calling it that so that when we are checking when they
shoot again we’re checking to see if they’ve already got one a trajectory in
the game and if they have we can delete it and add in a new one so that’s why we
did that next we’re going to want to change the
transparency of the trajectory so it’s reject or adopt transparency we want it
to be see-through so we can set the transparency to not point five so half
visible then we want to anchor it so that it stays in the same position
throughout chapter II lots of anklet equals true and then you can say shrub
Jack Tory doctor locked equals true so that it can’t be edited by anything else
or touched and then we can say trajectory dot can collide equals false
so that it can pass through objects and then finally we want to change its size
so it looks like a mini bullet trail now the correct size which I found was not
with three studs by no point three studs and then for the set value that will be
the length of the trajectory so the distance which we made earlier so we’re
going to say trajectory dot size equals vector three Don you open bracket close
bracket not point three comma not point 3 comma
distance just like that once we finished we can because we’ve done all the
properties now there’s nothing asked for us to edit except for the C frame now
because the player shoots this from their bullet I want the bullet trail to
stick with their gun for a couple of seconds and then it will disappear so if
they start moving after they’ve shot the bullet trail will still be connected to
the gun for a couple of seconds to do this I’m going to have a for loop which
will iterate from 0 to the length of the trajectory and it will go up in sixes
every time and then that’s just like a time keeper and it’s just a lot it will
explain itself in a moment so I’m gonna say for I equals 0 distance
6du and then I’m going to have an end and I’m going to say trajectory dot C
frame equals C frame dot new open bracket tool dot handle dot C frame P so
getting the position of the tool which is the starting point and the second
point will be the position which we sent over here so where it ended so the part
which it touched and then we’re going to want to close that bracket off and we’re
going to want to multiply that by another C frame so C frame new 0 0 but
for the Z value you want to say – distance right I want to divide this by
2 now the reason for this is because I’ll explain this for loop now we have I
which is our iterator now it starts at 0 and it ends at the the total distance
right because we may we define distances of variable up here and it’s basically
the tall position take away the position of the
we were a hit were they great so kind of like a difference between these points
how long is it so for a couple of seconds it takes it takes a couple of
seconds for it to get from zero to the correct distance if it’s going up by six
every time so say for example the distance was 300 okay and we’re going up
in six is every time so six 12 18 24 30 36 until it got to the distance to 300
right this gives us a few seconds for the trajectory to keep updating its
position to difference between the handle and where it hit and then we
multiply it by this new C frame value which is the distance so that divided by
2 so that it stays connected to our bullet right the bullet will hit it will
stay connected to the gun and when we move the seafoam position edits itself a
bit confusing I know but you’ll see how it works when the gun is finished and
because we’re multiplying C frames here what we’re doing is basically it’s
inverting its inverting forwards and backwards so we want it to go forwards
right we want the trajectory to still go forwards so we have to had add a minus
to the distance here and we’re dividing it by 2 so that the cuts out the
distance say it’s 310 to 150 but instead of it going backwards 150 we add this minus sign so it goes forward by 150 so
that we can still see the trajectory after it’s hit something for a couple of
seconds now it’s quite confusing I know it took me a while to it takes you a
while to work out when you can’t see it when you’re scripting it but it should
make sense in a moment but we want to wait some seconds now so we can actually
see the change happening so I’m going to wait not point naught naught naught 1
seconds then we can X out with this for loop and
just get out of it and then after this for loop we can get rid of the smoke so
we can say smoke turn on destroy open bracket close bracket and then what we
want to do now is we want to actually find the part which it touched and if
it’s a player we want to damage them so to do this we’re going to drop a line
and say if parts then if there’s a part which hit then we want to investigate
more about it so if parts named so part name equals
equals head this is doing the head shot okay so if we hit a head or we hit their
hats or part colon is a open bracket close bracket and there’s speech marks
hat and part dot parent code on final first child open bracket close bracket
humanoid right in a pair of speech marks dot health is greater than zero then
drop a line now this is what this if statement is doing so if we fit a head
so the part which we’ve here is called head or the part is a hat using is a
allows us to check the object name so the actual object so if it’s a hat
because there is a custom object for hat and if the player is alive so the parent
is you find a humanoid and it’s alive isn’t dead then we’re going to want to
fire a headshot now this is the remote event event we didn’t do in the local
script which I said we needed to wait until we’re done it in the normal script
here now this is because we have we found a headshot we’ve got a headshot
with hit someone in the head and it’s a headshot so let’s have a comment boom
headshot so we now want to get replicated storage
and because we want to show something on the GUI that’s in the player and a
server it would be best for the server to access the players GUI via a remote
event so we can say replicated storage got head shots now instead of saying
fire server with firing it from the server to the client this time so we
want to fire client so it’s going from the server to the client you use fire
client it’s going client to server you use fire server so headshot code on fire
clients open racket player close bracket and we’ve passed them through that
parameter of player again because we’re giving it the player who we want the GUI
to show up on the GUI sorry and then we want to change the damage which we give
to the player 250 which is half of the players health so if you get a headshot
you do more damage and then we want to put end we want to jump out of this the
first end here but still in the first if statement we’re going to want to get the
humanoid of this player so local humanoid equals part dot parents : find
first child whoops find first child open bracket close
bracket and in speech maps humanoid now the humanoid is what makes a roblox
player come to life it houses the health and everything else like walk speed
which is needed for a player to to work in roblox so once we’ve got the humanoid
we can then do another check so if not humanoid then humanoid equals heart doc
parents doc parent : find first child open bracket humanoid in speech marks
close bracket now what is what this is doing is we’re getting the humanoid by
getting the part the parent of this part because the part is inside a player and
so is the humanoid so to find this humanoid we have to find
first go go to the parent of whatever this part is inside and then check if
it’s got a humanoid insider that tells us that it’s an it’s a player because
each player has a humanoid inside it but if there’s not a humanoid there then the
part must be inside part that we hit must be inside something else which is
inside that player so it’s nested inside of something else
so we then we know that the humanoid will be the parent of what currently is
the parent of whatever is housing the part so we say part parent or parent
curl and find first child humanoid because it’s not if it’s not the parent
of something it’s got to be the parent of a parent of something now it might
not make sense but it that’s how it works because the humanoid is definitely
inside a player we just have to find it somewhere so after this we can drop
another line and then what we’re going to do is do what we’re doing now right
so we’re going to have another if statement so if you monoid then in fact
we could just say else go let’s just say else else so if there’s a humanoid then
we’re going to want to make sure we’ve got another which let’s just make sure
our indentation is correct if if and else okay so we need another engine here
there we go so let’s drop a line and just make sure everything’s indented
correctly I think it is yep if you need to indent your code click on tap and it
will move it forward so if there is a humanoid we want to damage that humanoid
so we can say humanoid : take damage Open bracket close bracket
and then we’re going to say damage inside of these brackets so it’s going
to damage this humanoid by the amount of damage which we put in the variable and
if they hit a headshot before it’s taking damage we’re setting that down
that two-fifty not thirsty and so now they’ve
taken damage we want to check if they’re dead
so if humanoid dot health is smaller than or equal to zero
then we’re gonna drop a line and inside of here this if statement we’re going to
increase the knock out the kills value by one so you can say Claire gotta lean
to stats got open bracket close bracket okay
oh value remember we don’t know what the ko value is called yet because you are
setting it yourself and we’ve got it in a variable so we put it in square
brackets so player don’t need to starts open square bracket okay over our you
close square bracket value equals and then we can copy this again
so we’re referencing the value and we can just say plus one and then what we
want to do is we want to increase the number of deaths for the victim by one
now if you reset yourself the if you just kill yourself it’s not going to
work you’re your wipeouts are not going to increase but I did that for a reason
because there are lots of scripts which you can find that do that for you or you
can just add a humanoid died event which which is pretty simple and you probably
have your own kills and wipeouts leaderboard already so that’s very easy
to do so let’s just head back into the main script so we’re going to say game
got players and then we’re going to in square brackets we’re going to say
humanoid dot parent dot name so we’re getting the we’re getting the player who
has the humanoid inside humanoid peridot named dots leader stats and then in
square brackets we can have W o value the Y Park value value so we’re getting
the value of that value which is pretty weird it might not make sense but
each value say for example this one the bullets value it has its own value
property down here so if you had a value called wo value we’ll have to get the
value of that so you might want to call it something different but to for it to
make sense with what I wrote you you should keep it wo value suing say game
top players humanoid peridot name not lead to stats
wo value value equals we’re gonna copy all of this and paste it and say plus 1
so that is the Lea stats completed now if you don’t want leader stats in your
game which is understandable maybe you don’t need them or you don’t want them
all that you have to do is just add double – before these lines line 42 and
43 and you also want to comment out line 3 on me and line forth like that but if
you do want them you can keep your script like mine and keep the lines in
there okay so that is the leader stats all done all that’s left to do now is
delete the trajectory and then we need to work on the animations so we’re going
to jump out of these if statements so we’re going to go 1 2 3 and we’re going
to want to write we’re going to want to have a space between the first two ends
and the last 3 ends so on line 46 here we’re going to want to wait 5 seconds
like what 5 seconds sorry and we’re then going to want to check if there is a
trajectory so tragic story then we’ll go on and delete it by same trajectory curl
on destroy open bracket close bracket okay so that is the shoots event the
shoot event remote event event done that’s quite hard to understand but it’s
the first remote event completed so when they shoot
it’s now working and our bullet trail will work and the player will receive
damage and we’re able to do head shots as well all that is left to do is we
need to add in our headshot remote event fire server on the local script we also
need to create animations and need to do the check body type as well and that
will be everything so let’s head back into the local script and let’s create
our head shots fire server remote event function so once you’re back in the
local script what you’re going to want to do is scroll down to the end of the
script and you’re going to want to say headshot dots on clients events because
it is being sent from the server to the client so instead of on server event we
do on client event so waiting for it to be triggered code on connect open
bracket function open bracket close bracket and drop a line now inside of
here what we’re going to do is we’re going to tween the gun GUI now I did a
video on tweening if you want to see it you can click on the eye the eye icon in
the top right of your screen so I’m not going to give a detailed explanation on
twinning but I’ve got some code which I’m going to copy in and I’ll explain
what it does so we’re getting the player who the local player and we’re getting
their player GUI where the gun GI s– stored we’re finding the headshots text
label and we’re tweeting it from the position which is off the screen to this
new position which is in the middle of the screen and these are the properties
of that tween so it’s going outward and it’s in the Quint animation style and it
lasts for naught point three seconds it will stay on screen for one-and-a-half
seconds and then it will back off the screen again and then once
we know that it’s off the screen it will reset back to its default position on
the right so it can’t win again because it’s got it’s coming in from the right
to the middle then it’s going off to the left but it’s off-screen we’re resetting
it back to the right where no one can see it so you’ll see it once we’re
finished you’ll see it working and you’ll see what I mean so that is the
headshot event completed so we now need to create our animations now I created
the animations myself which means you can’t take them because robots currently
limits users from taking others animations which isn’t very good so what
I’m going to do is I’m going to speed up a video of me making the animations and
you can slow it down in YouTube or quickly watch to get an idea of how I
made mine I also had an animation tutorial I’ll put it in the description
below for you to watch so here we go here’s me making the animations okay so
to animate you’re first going to want to get the roblox animation editor plugin
I’ll leave a link to that in the description for you and you’re going to
want to click on manage plugins then find plugins and you will be able to
search it there and install it once you’ve got that installed you will need
to restart roblox studio and you will need to then you should click on the
plugins tab and you’ll be able to see this area here now you’re going to want
to click on a rig builder and this GUI will pop up now we need two animations a
shooting and a reloading animation but we need two for our 15 and two for our
six so that’s four animations in total so I’m going to start off with the are
six animations I’m going to click on lock rig so click on block rig block rig
and drag the dummy down onto the base plate once you’ve done that you’re going
to want to click on the grey cog and arrow button
called animation editor you’re going to want to click on the torso and then
click select now this animation the shooting animation is
quite simple for our six in the first keyframe we’re going to take the arm and
we’re going to drag it upwards at ninety degrees just like that I’ll make it
straight there we go and we’re going to want to take the other arm and place it
so that it is supporting the the other arm so I’m going to have it straight and
I’m going to then tilt it and I’m going to press R to change it to the movement
movements editor I’m just going to tilt it a little bit more like that okay and
I’m going to then move it again and now I’m going to use local space by pressing
Y this defaults the movement tool there we go so that I can move it left or
right easily and there we go that looks fine I’m happy with that so what I’m
going to do is I’m going to take this keyframe and I’m going to click on the
pink icon I’m going to click on copy and I’m going to move the red timeline to
about noir point five seconds and then I’m going to click on edit and I’m going
to click on paste now once I’ve done that we’re going to click on edit again
we’re going to click on set looping and we’re going to set this to true so that
it loops until I own and equip the tool we’re then going to click Edit again
I’m going to set the priority to movement so that it overrides any idle
animations which play when you’re afk so that you can you’re always going to be
holding the gun now that’s a very simple animation if you wanted to make the legs
move you could go a bit further and make them move yourself but for now this is
all we need for an equipped animation so you’re going to want to click file
export and then follow the the follow the window which pops up to publish your
animation now once you have published your animation you’re going to want to
find it on the roblox website you can find it by going to the develop page
okay so go to roblox comm flash develop or click on this button here then you’re
going to want to click on animations at the side and find your animation click
it and you’ll be taken to your animation then you’re going to want to go up here
find the animation ID copy it head back to roblox studio and then you’re going
to want to scroll down find your in fact no we’re not going to do that we’re just
going to hold on to the animation ID so keep it safe somewhere so you can recall
it when we script it because you don’t got you don’t want to get mixed up with
your ar-15 and the r6 animations so make a note of that one or just keep uploaded
keep the tab open and then move on to the next animation we’re going to move
on so we’re going to click on new click on ok and we’re then going to want to
create a reloading animation r6 to do this we’re going to use the I’m going to
use the same player dummy and we’re going to press R so we go back to the
rotation mode and then on the first keyframe we’re going to want to drag the
the arm up straight on the left side oh yeah and then once you’ve done that
we’re going to want to move the keyframe to 0.1 and at this point we’re going to
want to take the other arm and just slightly move it and tilt it so it looks
like over time it will be gradually moving so I’m going to just tilt it a
little bit like that I’m going to move it ever so slightly just like that then
we can drag the keyframe to about naught point six not 0.65 and once it’s that we
can then move the arm again we can move the arm this this arm here a little bit
more forward we can tilt it a tiny bit more so it looks like we bring bringing
the arm up with more ammo okay and then at not 0.85 we’re going to drag it again
and we’re going to move the arm even further in and further up like that and
then at one point one we’re going to drag the keyframe and we’re going to
move it up and we’re going to move it closer to the other arm like this and
then we’re going to press our and we’re going to press Y so that we can get the
world space movement and we’re going to drag this arrow backwards so it looks
like the arm has moved to support the other arm then one point three we’re
going to drag the keyframe along again and we’re going to just rotate the arm
upwards so it looks like we’re filling it with some more ammo and we’re going
to tilt it like that and then we can just at the
next keyframe jump at 1.35 we can move it ever so closer ever so slightly
closer so it touches the other arm and we can move it down a bit as well and
then at one point four we can bring the arm in again like that in fact what
we’re going to do is take the going to take the animation at one point three
and just move out a little bit more just like that and at one point four we can
move it inwards like that there we go I like that and then at one point nine
we’re going to bring it bring the keyframe over and we’re just going to
put the arm back in its original position so move it back so that it it
looks like it’s in its normal position if it’s not perfect don’t worry because
the players won’t notice it you just want to make it look normal again like
it was that the gun is equipped so we just want the arm to go next to the
torso and you can line it up and rotate again so it looks like it hasn’t moved just bring it out a bit more and tilt it there we go I’m happy with that
so once you’ve done that I’m going to done I’m just going to look how I did it
yeah so there we go that is our reloading animation complete and if we
play it you can see that we reload the gun starts off slowly and then we insert
the ammo and then it goes back to normal position all right so that is the r6
shooting and reloading animations completed so we’re going to move on to
the AR 15 shooting and reloading now so you go you’re going to do the same thing
click on file exports and follow the instructions on the window which pops up
and then you’re going to use the steps which I showed previously to find your
animation ID and you’re going to note that down okay so now that we’ve
finished we can X out of the animation editor click OK
you can delete our dummy and we can do the same again but for an ar-15 rig type
so click on the rig builder icon click ar-15 lock rig wait for it to insert
drag it down and then position your camera so you can see it and then open
up the animation editor and click on this root part here then click select
and you’ve opened up the animation editor for your ar-15 body once you’ve
done this you’re going to want to find the first keyframe which is loaded up
and you don’t want to move it you want to take the left arm and you want to
lift it up just like we did with the last one with the r6 and you want to
then press R to get the movement grid open I just want to position it
at 90 degrees and once you’ve done that you want to bend the limbs a little bit
just to make it look more realistic so click on the next limb and just move
that one up a bit and I’m gonna move this one down a bit as well just so it
looks more realistic for the ar15 body because it has more limbs then you’re
going to want to take this on and the top the top bit of the arm the first
limb and you’re gonna want to lift that up and move it down or a pressing R and
then press R again and we’re going to want to rotate this one about 45 degrees
maybe third we have 30 to 40 degrees and then we want to move it tilt it towards
the other on and we’re gonna move it down a little bit as well and then we’re
going to tilt the other limbs just like that and then the last limb here like
the hand we’re going to move that up a tiny bit and we’re going to tilt that as
well just so it looks like the hand is
supporting the left arm here then we’re going to want to select the last lip the
first limb and we’re going to want to press R to move it back to the movement
tool and we’re going to take this arrow and just move it forward a bit like that
so that it’s touching the other arm and we will just move the second limb up so
that it looks like the the arm is going this is going under the first one and
supporting it and then if we play this animation actually first we’re going to
click Edit setting set to true and we’re going to do edit again and set the
priority to movements once you’ve done that we can play the animation and it
goes into that position and it stays like that forever
until we do something as such as unequip the
animation which will handle in the script so that is the ar-15 version
completed we’re going to do what we did before and click file export and you
will go through the steps to export your ar-15 shooting animation ok and for the
ar-15 reloading animation you don’t want to open up another block rig for ar-15
and make sure you have the animation editor studio open now once you’ve done
this we are going to create the animation which is it’s like are
equipping animation but it shows us filling the gun up with more arrow
that’s what it it’s going to look like and it won’t stay and it won’t stay
forever until we unequip it it will only last for a couple of seconds so it won’t
be looping so the first thing that we’re going to do is take the left arm and
we’re going to lift it up at about 90 degrees like that and if you want you
can move it down as well I’m just going to do that and once you have done this
what you can do after that is drag the slider across to not 0.5 seconds and
once you’re there you’re going to want to basically position the this this arm
downwards a little bit so I’m going to select the second limb and bring it down
and I’m going to take this arm and just rotate it like that
I’m also going to lift it upwards and once there like that we can drag the
slider to about not point 65 and we’re just going to keep turning both arms
around so that they’re touching each other and then we can make this arm look
as if we are pushing more ammo into it so what we’re going to do after we have
done that we’re going to move this arm in woods I’m gonna take the second limb
to bend the arm around just like that and after that we’re going to want to
rotate the left arm around so the the characters right arm but our left I’m
just going to rotate it I’m going to rotate the second limb as well and once
we have done that we can move the cursor along to about not point eight okay so
you can see already that we’re starting to the arms are starting to bend which
which is good we’re going to move it to naught point eight and what we want now
is we want we’re going to want to move this arm upwards upwards and so it
supports the left arm so we’re just going to start moving it across even
further and I’m going to take the rotation tool by pressing R I’m gonna
get rid of it and put this move tool in and then I’m going to change it to local
space by pressing white I’m just gonna move it forward like that I’m gonna move
it down as well okay let’s play it back and get this animation you can see it’s
starting to touch the other arm so what we really want to do now is just push it
into the other one so we can bend it upwards from about one point one second
yeah what one second or one point one will do you just want to bend it back
around and I’m going to just lift it up like that rotate it a bit more just like
that so let’s have a look at this again I’m gonna replay it and if I click on
plate whoops that did not go right I think that’s a bug in the animation I
deserve but I but you can see it reloads like that it
looks like we’re putting ammo into the gun and then we can just about 1.8
seconds we can just put the arm back to normal
so let’s bend the limbs back and we can then end the full arm back to its
original position move it back it doesn’t have to be perfect as players
won’t really notice the difference and if you want you can always make your own
reloading animations I’m sure yours might be a bit better than mine because
you will probably spend more time on yours them than I have but still
animations are animations and they make your game look more realistic so it’s
very good if you use them okay so I’ve put the arm back to you normal and if
you want you can put the other arm back if if you really want I’m just gonna end
it back to 90 degrees well bent bend it back to be straight so it’s tilted at 90
degrees as we will still be holding the gun remember when we stop reloading nope
that will do so I’ve got my reading animation I’m going to click file I’m
gonna then click export and you’re going to follow the same instructions which
will come up on your screen to publish your animation and then you can follow
the steps I showed earlier to get the IDs make sure you’ve noted down all of
the rd the IDS for the r6 and are 15 shooting and reloading because we’re
going to be needing them now in the scripting so once you’ve got your
animations you can close down the animation editor and then you can get
rid of this dummy you won’t be needing to do anymore animating and then
what you’re going to do is head back into the service crypt service into the
main script and we’re going to want to start handling the animations so outside
of this last end with the closing bracket you’re going to want to drop a
line or two and we’re going to now work on the equipping animation so we’re
going to get the remote event which we are calling from the local script we’re
going to look out for that to be called and when it has been we’re going to run
the animations on the play up from the server so to do this we’re going to say
replicate storage dot equip animation colon on server
sorry note dots on server event : connect open bracket function open
bracket player comma animation close bracket and drop a line and you should
have an end added in for you automatically
so we’re grabbing the remote event which we’re calling on the which we’re calling
from the local script when we equip the tool and when it has been called any
code inside of here run and we’ve got the two parameters which had been passed
so the player who called it and the animation which were going to run inside
the player so we’ve got to load in the animation so I’m going to create a
variable called new and them so the new animation which we’re going to load in
and you can end with capital eight equals game dot workspace and then in
square brackets because we don’t know the name of the player we’re going to
say player dot name because we’ve got the player parameter passed here player
dot name outside of the square brackets we can get the humanoid dot humanoid :
load animation open bracket close bracket and
inside of those brackets we need to tell the script which animation we need to
load in luckily we have as a parameter so we don’t need to do any extra digging
we can just write animation and there we go we have been able to load in our
animation so let’s make this local because we don’t need it anywhere else
in the script and we’re going to drop a line and I’m going to play the animation
by saying new and in code on play open bracket close bracket and there we go
that is the equiping animation completed next we’re going to want to take the
unequip animation a remote event which will be called by the local script again
and for this remote event we will make the animation I disappear and it we will
basically stop it so we’re going to say replicate storage and dot unequip
animation dot on server event : Connect Open bracket function open
bracket player comma animation closed bracket and drop the line and then any
of the code inside of here will run when we
unequip the tool because when we unequip it in the local script we are calling
this event and the server is watching out for it here and when we do it we’re
passing these parameters of player the player who called it and the animation
which we’re going to stop and we’re going to write some code which will make
the animation stop so we’re going to say new Anam code on stop open bracket close
bracket and then we want to delete any trajectories which were made by this
player because they haven’t got their tool equipped anymore and they will
become useless so we’re going to use a for loop to index everything in the
workspace and check to see if it is trajectory and if it is we can’t destroy
it so to do this we’re going to say for AI comma V in pairs open bracket close
bracket do drop a line now we’ve got eye which is our iterator it will go up
every time we index something and V is the current object which the script will
be indexing so if there’s three items in the workspace I will be one on the first
time it indexes something and V will be the thing which is indexed so if it’s
indexed apart I want to change its color we can say V color I brick color equals
a red so that’s what I and V are you can call them anything you want but the
first value is the iterator and the second value is the object which you are
indexing so inside these brackets we want to index the workspace so we’re
going to say game don’t workspace : get children okay so we’re getting
everything inside at the workspace and then we have them do because that’s the
syntax it’s what we need to tell the script it’s a for loop and we’re going
to do an if statement to see if the players name is equal to the player name
which is on the part name so if V dot name so if the objects name is equal to
player dot name because remember we have the player name in this parameter here
so we don’t need to worry about getting it we don’t know it yet and remember how
we called the part player names trajectory so if it was shedletsky it
would be called sure let’s keys trajectory okay well we’re basically
doing a check here to see if the objects name is equal to the player whose tool
who just own equip their tool if it’s called player name dot dot and then in
speech marks we can have apostrophe s trajectory so we’re using string
concatenation here to bind these two these values together so the players
name we’re binding it with this this string here so it all becomes one big
string should let skin’s trajectory for album blocks a trajectory and we’re
checking if that’s the same as the parks name and if it is so then we can just
destroy it so V : destroy because we don’t need it anymore because they have
unequipped at all okay so now what we need to do is we need to exit out of
this an equip animation event so we’re going to find the end with a closing
bracket and drop a line now remember this is still inside the equip animation
event so make sure that is still in there because we want to work on the
reloading event now we’ve got all of these events which we’re going to be
scripting we have done the shooter vent the unequip animation and the equip
animation we we just need to do the headshots
the reload which we’re doing now and the fetch bullets left I think we’ve done
and how we need to do the check body type as well I think okay so we’ll do
that now and that should be the end part from the gun GUI we need to work on that
as well so outside of the unequip animation event still indented with the
tab because we’re still inside one event we’re going to get the reload event so
we’re gonna say replicated storage don’t reload dot on server event so when it’s
been called by the client : connects open bracket function open bracket close
bracket and we’ve got the same parameters player comma animation
because that’s what we sent from the client to the server then we can drop
line and have this end engine for us and what we want to do when we reload is we
want to stop the current animation we have so
the the animation we have up here which will be the equipping animation new an
e-coat I’ll stop we’re just stopping the animation because we want to do a new
animation the reloading animation so we want to do the same thing which we did
up here we can copy these two lines of code line 55 and 56 which are the
animation lines which are adding in the animation so paste them in but we want
to change new Anam to reload on him now this is going to be the reloading
animation so just y-you see a new onion on these two lines replace it with
reload on him with a capital A for animation for anime that’s all we need
to do for the reloading event because we just need to stop the animation and play
a new one what we then want to do is work with our remote functions and we’re
going to be doing Czech body type now I mentioned earlier that there difference
between remote events and remote functions is that remote events they
they are called on the client just like remote functions but they only tell the
server to do something now a remote function tells the server to do
something from clients but then the server will feed back to the client with
some extra information now in our case we are asking the server to to check the
plays body type and then return that body type to us so it’s feeding back the
body type that’s why we’re using a remote function so we’re going to do
check body type in the main script and we will be able to feed back to the
local script so to do this we’re going to actually create a function because
you can’t use events for remote functions you have to use a normal
function so to do this so we’re going to firstly write a function called check
body type so function check for D type so
lowercase C for check and then uppercase B and uppercase T for body type and then
we’re going to have some brackets and the two parameters for this function
will be player comma tool so we’ve got two two parameters
because in the gun did you do yes area in the local script in the in
the gun we had a function which would invoke the server for the check body
type remote function and we passed the parameter of tool and remember I said
earlier how when we call something from the client roblox automatically sends
that the player with it first as an extra parameter and that has to go first
that’s what I’ve got player here and then after that we’ve got the parameter
that we set so we’ve got two parameters we’re gonna drop line make sure the end
has been added in and then inside of here we’re going to do a check to see if
the player is r15 or r6 now you may not know this but if your r6
your torso will only be one part called the torso but if your r15 your body is
made up of multiple different parts and let me just show you that this is an r15
body as you can see it’s got lots of different joints and you can see the
torso here is split up into two parts we’ve got the this bit here and we’ve
also got this link this other part underneath it here so let’s just open up
my body and you can see I’ve got a lower torso which is this part and I’ve also
got an upper torso so we don’t actually have a part named torso whereas with r6
we would just have one solid part called tor so now if we used our six just for
this gun tutorial and we only use two torso not upper torso or lower torso it
wouldn’t work on our 15 because there is no torso net part there’s no part name
torso so we need to make sure that we check
to see if there are six or r15 and feeding back to the local script so that
our game can work for both body types so to check if they are are 15 or 6 we can
just check if they have a low torso and if they don’t there are six so we are
going to see if game got workspace open square brackets and close them off and
inside these square brackets we’re going to put the player dot name because we’ve
been given the parameter up here and we can get their name and then we want to
use something called find first child now find first child is similar to just
putting a dot and then the thing you want to get but the difference is if
it’s not there some reason it won’t throw up an error it will just ignore it
so that’s the good thing it won’t break your script so we can say : find first
child open bracket close bracket and then in speech marks we can say lower
torso and then we can just close off the brackets and put them so drop a line and
make sure you’ve got this end in here so if they’ve got a low torso then we’re
going to put a comment and say that there are 15 because they definitely are
15 we’ve found a low torso in them so they’ve got to be are 15 so what we can
do is change the animation ID of our shoot and reload animations which we
have here to the ar-15 type and this is why I asked you to note down your
animation IDs if you haven’t got them you can get them off the roblox website
off the develop page and earlier on in this tutorial remember I showed you how
to do that so make sure you’ve got your IDs written down because we’re going to
be needing them now so if there are 15 we want to change their shoot animation
ID by same tool dot shoot but animation ID equals and then in speech marks we
can say our px assets ID colon slash slash and then you’re going to want to
pups your ID for the animation after these forward slashes just paste
it in and there you go you’ve got your animations loaded in you
will then just going to want to do the same thing again for the reload
animation so we can just paste it on the line below but change shoot to reload
and change the animation ID to the ar-15 reload animation id just paste it in and
once you’ve done that we’re going to want to tell the local script that we
have successfully checked that we are our 15 I want to tell it that but the
body type is our 15 so to do this we can say return and then in speech marks as a
string our 15 that is all you need to do so we’re just going to copy this if
statement again we’re not going to do else because there could be other things
making them not have a lot or so for example they might have died and the
torso has been destroyed and that could happen at the same time the script is
running you have to cater fool of these things that happen so it’s just better
to have it as an extra if statement when you’re unsure so we’re just going to
change lower torso here torso and the comment
we can change our 15 to our 6 because if they got a torso they’re bound to be are
6 and you’re just gonna want to copy your animation IDs in like this and
you’re gonna want to return our 6 instead of our 15 so if they’re not our
15 it will check to see if there are 6 instead they’re bound to be one of these
so it’s going to return either ar-15 or our 6 so that is our check body type
completes we just need to bind it up now to the on server invoke event which will
run whenever the local script has called it because we can’t do an event like
remote events we have to use functions so we can just say replicate the storage
not check body type because I believe that we called it check body type with
capital C BNT yep and then we’re going to want to say doctor on server invoke
and then instead of saying : connect business isn’t an event it’s a function
we just want to put an equal sign and you won’t want to write check body type
like this function name up here remember because we called our function check
body type with the lowest C you want a capital here and a lowercase C here so
that is the check body type function all done
so now we have done our check body type remote function we’ve done our reload
remote event we’ve done our unequip animation we’ve done our equip animation
and we’ve also done our shoot event so all that we have to do now is the gun
GUI and we should be finished ok so I discovered a few bugs while testing out
the gun which you need to know about so down here on line 84 when we are binding
the function to the remote function event down here where you don’t want
these brackets because we’re actually just wanting to call the name of the
function and not the brackets so get rid of those and then in the local script I
also noticed that we have our variable here called reload okay which is the
your motr vent okay but down here we also have a function called reload and
the the script won’t know if we want to call the function or the variable so we
need to change the variable reload – reload – and then down here we want to
find the reload function and when we’re firing and using fire server to
call the server we want to change reload – reload – so that the script doesn’t
get confused you can play the game and when we load in and equip the gun we’ve
got our animations working fine and if we we haven’t set up the bullets yet so
let’s just give ourselves some bullets by heading into the gun and changing the
bullets value to up no 100 and let’s start shooting you can see we are
creating some rays which will kill people and the bullets value is actually
going down as we click and so once it gets to zero it won’t shoot any more
bullets so that’s that’s pretty much most of the scripting done for for any
visual things that you’ll see so for the shooting for example and there we go
we’ve run it up on its own we can’t shoot anymore and we’re gonna quit the
gun and the trout the trajectory goes away and let’s just give myself few more
bullets and let’s look at the workspace so when I click you can see it got my
player name and insert of the park called Alvin blocks his trajectory there
were me did the string concatenation earlier we called it player dot names
trajectory so you can see how it has swapped out the values fought for my
actual name and that’s why we put it in square brackets okay so now on to the
last part of the scripting what we’re going to want to do is set up the gun
GUI oh and I also forgot to tell you that the reloading works as well which
we worked on so if you press R your gun will reload and you’ll get extra bullets
so we just need to start making this bullets GUI which will countdown when we
keep shooting so we’re going to X out of the game and head into the gun GUI and
let’s get started with the bullets we’ve got our local script in here but we want
to have it actively update eating – the amount of bullets left so
we want to get the bullets value which tells us how many bullets we’ve got in
our gun so to do this we’re going to make a local variable so local well it’s
equals game but workspace cut um wait to try open bracket close
bracket because we have to wait a bit of time for the player to enter the game to
be all fully loaded and we’re going to say game drop players got local player
dot name inside of here so getting the player in which this script is running
on we’re getting their name putting it inside these brackets because we don’t
know their name yet and then we’re going to wait for that gun to be added so code
on wait fourth trial remember you need to capital W F and C for each word wait
for child and then inside of these brackets we’re going to put in speech
marks gun and then we can’t get the bullets value by saying bullets
remember this gun is going to be inside of the player when they join the game so
we’re just get accessing this gun inside of there player when they’ve equipped it
and we’re getting their bullets value because because we because when they
join it won’t be there straight away it’s when they equip it that it all land
in their player it’ll be in their backpack so just waiting for it and once
we’ve got the bullets value we can update the text labels text straight
away to tell them how many bullets they have sewing say scripts dot parents drop
text equals and then in speech marks we can say bullets with a colon and a space
and then after that we can do string concatenation to link up the amount of
bullets we have left to this text label so to do this we’re going to say dot dot
bullets dot value just like that so that’s going to change
the text of the text label to bullets and the number of bullets we have left
and then what we want to do is use a changed event to see if the value of the
bullets is changed by the local script and if it is we want to update this text
label straight away so that the user knows how many bullets they have left so
to do this we’re going to say well it’s not changed because we can’t say bullets
value we have to use the bullets value property object because there are lots
of values which could be changed so that the name the value but we know that
nobody’s going to change the name of this bullets object so we know that if
it gets changed the values changed so when it’s not changed colon connects
Open bracket function Open bracket close bracket drop a line and then we’re going
to do an if statement to see if there are of bullets we’re going to say that
they have to press R to reload or we’re just going to put their actual number of
bullets so if bullets dot value equals equals
zero then script or parent dot text equals and then speech marks out of
bullets press R to reload now you can put anything inside this string you
could say reload or just out of bullets I’ve put both because I want the user to
know exactly what to do so or to reload or mobile button so if they’re on mobile
they can press the mobile button okay and then outside of that if statement we
can in fact what we’re going to do we’re going to say else and you’ve then we’re
going to drop a line and have an end so inside of this bit if the bullets aren’t
equal to zero we can just update it with how many bullets they have left so we
can say scripts got parents text equals and then in speech marks you
can say the same as we said earlier in fact we could just copy and paste this
we’re updating that the text label value to the amount of bullets they have and
we’re concatenating it with the bullets value so let’s test this out let me play
the game and you see we have got zero bullets so
and we can’t shoot so let’s press R and it’s updated to reloading because the
local script did that for us and when we shoot the amount of bullets is going
down and then we’re out of bullets what do we do well it says out of bullets
press R to reload all my Robertson so we press R and it starts reloading for us
and if we shoot again we’re able to have more bullets so this will work forever
infinitely until you die and then you’ll just respawn again with the gun but if
you wanted to have a bigger capacity for your gun you would have to go into the
local script and the amount of bullets which will be replenished you could set
that to 15 and equip the gun I’m just gonna reload it for now because we
haven’t worked on actually adding the the bullets when you begin but you got
15 bullets 14 13 12 11 10 9 8 7 6 5 4 3 2 1 so that’s how you do that I’m gonna
keep it at 6 because I am NOT gonna make it unrealistic by having lots of bullets
in it and that that’s the GUI all set up we’re going to want to select the
bullets value and update its value to the amount of bullets you want the
player to have when they first equip it so that’s 6 in my case and if you want
yours to be 15 set it to 15 but you can choose any number really it’s your gun
it’s your game you do what you want so once you’ve done that and with that I
think we have finished the tutorial so test out now and see our gun in action
so I’m gonna start up a local server with two players in it you can select
how many players you want I’m gonna enable filtering enabled and I’m going
to open up this server press Start and I’m gonna let it load up and hopefully
this this will work with filtering enabled or experimental mode turned off
so with this your game will be fully compatible for all ages so I spawn in
the game I’ve got another player here to test the gun on so I equip it and I’m
gonna shoot at him and I’m gonna do a headshot as well he dies the headshot
GUI comes up on the screen it flies away and can see I’ve gained one kill and he
has gained one wipeout and if I reload in fact let’s kill it let’s shoot him a
few more times and now I’m out of bullets so I need to reload so I’m gonna
press R to reload my gun and there we go and we have more bullets six more
bullets so I’m gonna shoot him again gonna get a headshot I’ve got another
headshot and with two kills that time he died
because we gave him a headshot whoops there you go he’s died now because he
regained a little bit of health but you can edit the amount of damage he gets
for a headshot and if we keep shooting him you see my kills go up and so does
his wipeouts so that is the gun and if we try it in the mobile emulator let’s
just ruin it on play so low you can see I get the gun and if I equip it you can
see I get this wonderful mobile art button for reload and I can move about
and I know there’s a zombie so I can reload if I want I can shoot I can I can
click on the screen to to shoot all right and then when I run out of bullets
I can press the mobile button and I reload
without having to press any keys and I can shoot him again maybe I can get
headshot there we go I got a headshot and my
kills went up on mobile as well so to recap we have just created a filter of
enabled compatible non experimental mode compatible gun which works on computers
and mobile devices with fully working ar-15 and r6 animations as well as a
custom mouse cursor and custom sounds as well the reason I didn’t have any sounds
playing was because of any potential copyright infringement but if you want
to put your own gunfire in you just need to select the gunfire sound in the
Explorer tab and then paste your sound ID in this box here and it will add in
your sound and it will play once you shoot your bullet so we’ve got custom
sounds and we’ve also got a kills and wipeout compatible leaderboard as well
and you’ll be able to save this data if you want by using my roblox data store
tutorial so that is the end I hope you enjoyed it and I’ll see you in the next
video so thank you for watching this roblox a gun tutorial if you enjoyed it
please do leave a like and if you want to see more roblox content from me
please do click the subscribe button down below and turn on notifications so
you do not miss anything from me this is Alvin blocks telling you to keep
scripting

Leave a Reply

Your email address will not be published. Required fields are marked *