Gun Tool Skeleton/Basics

A simple skeleton version of a gun that can be upgraded to be a lot better.

by NeonD00m

Author Avatar

Gun Tool Skeleton/Basics

-----------------------------------------

By NeonD00m

(This is my second Lua Learning article I will gladly take any feedback on the way I make them so please tell me what you think, enjoy!)

-----------------------------------------

Setting Up The Tool First we need to get the tool ready for coding so, we have to start by making a 'Tool' in the workspace then adding a RemoteEvent called 'Damage' then build your model. If you already have a mesh or union then you can skip this section. (IF YOU HAVE MULTIPLE PARTS PLEASE UNION THEM FOR THIS TUTORIAL)

Sorry for low-quality image but here is a short tutorial on how to make unions which can be useful if you don't have blender or like to build with parts. You are going to need to select all your parts you need for the gun which should look something like what the black circle has highlighted, then go to the top and make sure you are on the 'MODEL' tab, and you should see 3 options under Solid Modeling (tell me in the feedback if you would like a full tutorial for that) don't about the others and just click 'Union' which is the first button. Then you have your union!

img|5065x3865

Coding The Gun: Part 1 Now to get back to the setup. Inside the tool insert a local script and a server script. We will leave the server script for later, because understanding the local part first is better for learning the material.

First let's start off with some variables. I used a couple variables to make it easier to organize all the statistics for the gun.

local maxAmmo = 5
local ammo = maxAmmo
local reloading = false

These variables should be pretty self-explanatory so now we can move on to the main scripting!

Coding The Gun: Part 2 Get comfortable because there is going to be a bit of coding here. First we can use the following line to start off:

script.Parent.Equipped:Connect(function(Mouse)

In this line we selected the tool and checked for if the player equipped the tool and in the paramters put 'Mouse' we will use this later. Our next few lines of a code are going to be to write a function 'Reload()' so copy that down.

local function Reload()
    reloading = true
    wait(2)
    ammo = maxAmmo
    print("Reloaded")
    reloading = false
end

We can call this function when we the player wants to reload. Now for the shooting part of the gun. I am just gonna show you all of this and quickly explain it because line-by-line this could take a while.

script.Parent.Activated:Connect(function()
        if ammo > 0 and not reloading then --SHOOTING
            ammo = ammo-1
            if Mouse.Target.Parent:FindFirstChild("Humanoid") then
                script.Parent.Damage:FireServer(Mouse.Target.Parent)
            end
        elseif reloading == false then
        Reload()
        end
        print(ammo)
    end)

As you can see the first few lines check if the player clicked and can shoot, then looks for a humanoid and uses a RemoteEvent we will setup later in the server script. We are almost done with the local script now! With UserInputService we can chekc if the player pressed a button on their keyboard. So let's put it to use!

game:GetService("UserInputService").InputBegan:Connect(function(Key) --RELOADING
    if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
        Reload()
    end
end)

What this code does is wait for the player to press a key then in the parameters, carries the key to ask itself does the player have maxAmmo, is the player reloading, and is this Key, 'R' and if it is, it uses our 'Reload()' function we made earlier. Now for the server script!

Coding The Gun: Part 2 Now edit the server script. This is gonna be a bit quicker than the local script. Here, we have to make 1 variable which is 'damage':

local damage = 50

Now we have to make an event listener to sense if the RemoteEvent was fired so let's get the RemoteEvent and wait for it to be fired with '.OnServerEvent'.

script.Parent.Damage.OnServerEvent:Connect(function(Player,Target)
    Target.Humanoid:TakeDamage(damage)
end)

Then inside the connected function we put in a line that will damage the player with 'TakeDamage'.

And now your gun is ready for testing!

I have a better gun coded the only issue is it will be WAAAY more advanced and will take LOOOOOOOOOOONG, Tell me in the feedback if you think I should make a tutorial for it!

-----------------------------------------

Gun Tool Skeleton/Basics

-----------------------------------------

By NeonD00m

(This is my second Lua Learning article I will gladly take any feedback on the way I make them so please tell me what you think, enjoy!)

View in-game to comment, award, and more!