Garry's Mod

Garry's Mod

162 ratings
How to create basic SWEPS - a guide for lua noobs
By LegendofRobbo
How to create a basic SWEP using a pre existing weapon base (mad cows base in this example but once you learn this it's very easy to change to a different base)
   
Award
Favorite
Favorited
Unfavorite
Stuff you will need
1. Knowledge of how to edit lua files using wordpad or notepad+ (you do not need to know any actual lua as long as you are making basic bullet chuckers)

2. the SWEP construction kit which can be found here: https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=109724869
it is required to create the weapons ironsights coordinates (so you look down the sights properly instead of poke your eye out, try to fire from crotch level or something ridiculous like that)
Time to get down to buisiness
First of all i will do a raw copy paste of the weapon's code and show you what each function does and how to change them around to get the desired effect. We are using the mad cows weapon base as an example though most weapon bases look very similar to this so it's not to hard to change bases.

First things first, open the addons lua/weapons section and copy and paste an existing weapon folder (this assumes you know how to extract an addon from its .gma file to access its lua code) and call it the ENTITY name of your weapon. Entity names are what your weapon will appear as in the code and in the in game console, they cannot contain spaces or glitches will happen. Note that the entity name is NOT the actual weapon name that will appear in the Q menu, this will be defined later. An example entity name is weapon_mad_aidslauncher.


Each weapon consists of 3 lua files, named cl_init.lua, init.lua and shared.lua
opening cl_init.lua yeilds this:

include('shared.lua')

SWEP.PrintName = ".50 DESERT EAGLE" - This is what the weapon will show up as in your Q menu
SWEP.Slot = 1 - What slot the weapon occupies in the weapon selection menu (for reference slot 0 is physgun, gravgun and crowbar slot)
SWEP.SlotPos = 1 - Governs how far down the slot it will appear, higher numbers will place the weapon towards the bottom (its generally accepted practice give the more powerful weapons higher numbers)

// Override this in your SWEP to set the icon in the weapon selection
if (file.Exists("../materials/weapons/weapon_mad_deagle.vmt", "GAME")) then
SWEP.WepSelectIcon = surface.GetTextureID("weapons/weapon_mad_deagle")
end
^ the above code sets the weapon's icon in the q menu and weapon selection menu, you will need knowledge of how to create .vmt materials to use this properly

thats it for cl_init.lua
init.lua will contain only the following:


AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')

There is nothing wrong with any of this code so leave it alone unless you have some kind of fetish for flooding your console with errors.
Skipping past that we get to the real meat and bones of a SWEP, the file known as shared.lua:


SWEP.HoldType = "pistol" - sets how the weapon is held in third person. known holdtypes are:
ar2
camera
crossbow
duel
fist
grenade
knife
melee
melee2
normal
passive
physgun
pistol
revolver
rpg
shotgun
slam
smg

SWEP.Base = "weapon_mad_base" - what code base your swep uses, leave this alone for the purposes of this tutorial.

SWEP.Category = "Mad Cows Weapons" - the category your weapon appears in in the q menu

SWEP.ViewModelFOV = 70 - how far away from your body the weapon is held in first person, higher numbers bring the weapon closer. set it to 1 to have retarded slenderman arms

SWEP.ViewModelFlip = true - sets which side of the screen your weapon appears in first person, so that lefties can piss everybody else off by making left handed sweps

SWEP.ViewModel = "models/weapons/v_pist_deagle.mdl" - sets your weapon's first person model, must be a properly rigged and animated weapon model or things may get freaky
SWEP.WorldModel = "models/weapons/w_pist_deagle.mdl" - sets what model your weapon shows as to everybody else and on the ground if you drop it

SWEP.Spawnable = true - sets whether this weapon can be spawned from the q menu or not
SWEP.AdminOnly = false - whether or not you need to hold admin rank to spawn this weapon, this used to be called SWEP.AdminSpawnable but garry changed it recently

SWEP.Primary.Sound = Sound("Weapon_Deagle.Single") - what type of bang bang sound your gun makes when you fire it

SWEP.Primary.Recoil = 5 - higher recoil will jerk your view around more and make it harder to control the weapon when firing rapidly

SWEP.Primary.Damage = 25 - this is how much damage your weapon will do if you shoot somebody in the centre of their chest, keep in mind that headshots will deal twice as much damage and shooting them in the arms and legs will do reduced damage.

SWEP.Primary.NumShots = 1 - how many bullets are fired per shot, this is generally set to 1 unless you are either making a shotgun or have no understanding of how firearms work

SWEP.Primary.Cone = 0.02 - the base accuracy of the gun in degrees, 0 means you could shoot the balls off a fly at 500 metres and 45 means you couldnt hit the broad side of a barn with this gun

SWEP.Primary.Delay = 0.5 - the minimum delay in seconds between each shot, the smaller this is the faster your gun fires

SWEP.Primary.ClipSize = 7 - how many shots you can fire before needing to reload

SWEP.Primary.DefaultClip = 7 - how many bullets you get from picking the gun up, setting to 0 will make you always start with an empty gun

SWEP.Primary.Automatic = false - false means you need to pull the trigger for each shot and true means you can just hold the fire button down and the gun will keep firing

SWEP.Primary.Ammo = "CombineCannon" - what type of ammo the gun uses
the default ammo types are:


AR2 - Ammunition of the AR2/Pulse Rifle
AlyxGun - (mad cows base calls this "5.7mm Ammo")
Pistol - Ammunition of the 9MM Pistol
SMG1 - Ammunition of the SMG/MP7
357 - Ammunition of the .357 Magnum
XBowBolt - Ammunition of the Crossbow
Buckshot - Ammunition of the Shotgun
RPG_Round - Ammunition of the RPG/Rocket Launcher
SMG1_Grenade - Ammunition for the SMG/MP7 grenade launcher (secondary fire)
SniperRound
SniperPenetratedRound - (mad cows base calls this ".45 Ammo")
Grenade - Note you must be given the grenade weapon (weapon_frag) before you can throw grenades.
Thumper - Ammunition cannot exceed 2 (mad cows base calls this "Explosive C4 Ammo")
Gravity - (mad cows base calls this "4.6MM Ammo")
Battery - (mad cows base calls this "9MM Ammo")
GaussEnergy
CombineCannon - (mad cows base calls this ".50 Ammo")
AirboatGun - (mad cows base calls this "5.56MM Ammo")
StriderMinigun - (mad cows base calls this "7.62MM Ammo")
HelicopterGun
AR2AltFire - Ammunition of the AR2/Pulse Rifle 'combine ball' (secondary fire)
slam - Like Grenade, but for the Selectable Lightweight Attack Munition (S.L.A.M)


This is identical to the primary fire codes but it is for your guns secondary fire. Mad cow's base uses the seconday fire mode as an iron sights function so do for the purpose of this tutorial not touch this code or you will break ♥♥♥♥.

SWEP.Secondary.ClipSize = -1 // Size of a clip
SWEP.Secondary.DefaultClip = -1 // Default number of bullets in a clip
SWEP.Secondary.Automatic = false // Automatic/Semi Auto
SWEP.Secondary.Ammo = "none"



SWEP.ShellEffect = "effect_mad_shell_pistol" // "effect_mad_shell_pistol" or "effect_mad_shell_rifle" or "effect_mad_shell_shotgun" - defines what type of empty shells are ejected from the weapon when you fire, use "none" if you don't want this to happen

SWEP.ShellDelay = 0.05 - the delay in seconds between when you fire the gun and when the shell is ejected, this setting is perfectly fine for 99% of weapons so there is no need to change it

Continued in next section
Shared.lua Part 2
continuation of the shared.lua section:

hese are mad cows specific functions to define what general type of weapon your swep is, do not set more than one as 'true' or evil spirits will take over your gmod and cause it to spew errors
note: there is minimal difference between pistol and rifle, shotgun gives your weapon custom shotgun specific gunsmoke effects as well as sequential loading (you load one shell at a time into the weapon instead of loading an entire clip like a normal gun) and the sniper class gives you scope code and the option of making your weapon a bolt action rifle.

SWEP.Pistol = true
SWEP.Rifle = false
SWEP.Shotgun = false
SWEP.Sniper = false


SWEP.IronSightsPos = Vector (5.1434, -1.75, 2.673)
SWEP.IronSightsAng = Vector (0.2494, -0.0121, 0)

^ this is the postion your gun will be in when you look down the sights, you can acquire this data using the ironsights function of the swep construction kit.

SWEP.RunArmOffset = Vector (2.917, -18.901, -12.417)
SWEP.RunArmAngle = Vector (62.594, 0, 0)

^ same as ironsights but this is the position your weapon will be in when you are running with it or have it holstered.

optional/misc functions:

SWEP.ScopeZooms = {12} - this is only called if your weapon is a sniper, it is the magnification of your scope, eg 8x, 12x, 3.5x

SWEP.BoltActionSniper = true - this is only called if your weapon is a sniper, it governs whether you must cycle the weapon's bolt between each shot or not

SWEP.Tracer = 0 // 0 = Normal Tracer, 1 = Ar2 Tracer, 2 = Airboat Gun Tracer, 3 = Normal Tracer + Sparks Impact - exactly what it says on the can, governs what your bullets look like

SWEP.IronSightZoom = 1.5 - add this function if you want your weapon to have zoom while looking down the iron sights

SWEP.Penetration = true - add this function if you want to override whether the weapon's bullets penetrate thin surfaces or not, this is a mad cows base function and is not found on other swep types

SWEP.Ricochet = true - add this function if you want to override whether the weapon's bullets ricochet off hard surfaces or not, this is a mad cows base function and is not found on other swep types

SWEP.MaxRicochet = 1 - add this function to override the maximum number of times a bullet can ricochet



// Burst options
SWEP.Burst = false
SWEP.BurstShots = 3
SWEP.BurstDelay = 0.05
SWEP.BurstCounter = 0
SWEP.BurstTimer = 0
^ add this set of functions if you want your weapon to be a burst fire gun, this will only work on mad cows base



// Custom mode options (Do not put a burst mode and a custom mode at the same time, it will not work)
SWEP.Type = 1 // 1 = Automatic/Semi-Automatic mode, 2 = Suppressor mode, 3 = Burst fire mode
SWEP.Mode = false

SWEP.data = {} - no idea what this does
SWEP.data.NormalMsg = "Switched to semi-automatic." - override these to name your alternade firing mode

SWEP.data.ModeMsg = "Switched to automatic." - override these to name your alternade firing mode

SWEP.data.Delay = 0.5 // You need to wait 0.5 second after you change the fire mode

SWEP.data.Cone = 1 - this is the modifier effect of your alternate firemode, for example setting cone to 2 will make the weapon twice as accurate while in single shot/supppressed/whatever mode

SWEP.data.Damage = 1 - this is a modifier effect of your alternate firemode

SWEP.data.Recoil = 1 - this is a modifier effect of your alternate firemode

SWEP.data.Automatic = false - this is a modifier effect of your alternate firemode

^ add this set of functions if you wish to give your weapon an alternate fire mode that can be activated by pressing e and r together, remember to only add either this OR the burst fire code above, if you add both you will have a bad time

/*---------------------------------------------------------
Name: SWEP:Precache()
Desc: Use this function to precache stuff.
---------------------------------------------------------*/
function SWEP:Precache()

util.PrecacheSound("weapons/deagle/deagle-1.wav")
end

^ copy paste your SWEP.PrimarySound to here, it loads the sound into your computer's RAM which makes it lag less if the sound needs to be called a lot eg for a super rapid fire weapon

and thats all for now folks, happy hunting and be sure to see the next page for an example swep you can copy paste to speed up the process
Example Swep Code
paste this into cl_init.lua:

include('shared.lua')

SWEP.PrintName = ".50 DESERT EAGLE" // 'Nice' Weapon name (Shown on HUD)
SWEP.Slot = 1 // Slot in the weapon selection menu
SWEP.SlotPos = 1 // Position in the slot

// Override this in your SWEP to set the icon in the weapon selection
if (file.Exists("../materials/weapons/weapon_mad_deagle.vmt", "GAME")) then
SWEP.WepSelectIcon = surface.GetTextureID("weapons/weapon_mad_deagle")
end



paste this into init.lua:

AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')



paste this into shared.lua:

// Variables that are used on both client and server
SWEP.HoldType = "pistol"

SWEP.Base = "weapon_mad_base"

SWEP.Category = "Mad Cows Weapons"

SWEP.ViewModelFOV = 70
SWEP.ViewModelFlip = true
SWEP.ViewModel = "models/weapons/v_pist_deagle.mdl"
SWEP.WorldModel = "models/weapons/w_pist_deagle.mdl"

SWEP.Spawnable = true
SWEP.AdminSpawnable = false

SWEP.Primary.Sound = Sound("Weapon_Deagle.Single")
SWEP.Primary.Recoil = 5
SWEP.Primary.Damage = 25
SWEP.Primary.NumShots = 1
SWEP.Primary.Cone = 0.02
SWEP.Primary.Delay = 0.5

SWEP.Primary.ClipSize = 7 // Size of a clip
SWEP.Primary.DefaultClip = 7 // Default number of bullets in a clip
SWEP.Primary.Automatic = false // Automatic/Semi Auto
SWEP.Primary.Ammo = "CombineCannon"

SWEP.Secondary.ClipSize = -1 // Size of a clip
SWEP.Secondary.DefaultClip = -1 // Default number of bullets in a clip
SWEP.Secondary.Automatic = false // Automatic/Semi Auto
SWEP.Secondary.Ammo = "none"

SWEP.ShellEffect = "effect_mad_shell_pistol" // "effect_mad_shell_pistol" or "effect_mad_shell_rifle" or "effect_mad_shell_shotgun"
SWEP.ShellDelay = 0.05

SWEP.Pistol = true
SWEP.Rifle = false
SWEP.Shotgun = false
SWEP.Sniper = false

SWEP.IronSightsPos = Vector (5.1434, -1.75, 2.673)
SWEP.IronSightsAng = Vector (0.2494, -0.0121, 0)

/*---------------------------------------------------------
Name: SWEP:Precache()
Desc: Use this function to precache stuff.
---------------------------------------------------------*/
function SWEP:Precache()

util.PrecacheSound("weapons/deagle/deagle-1.wav")
end
72 Comments
BlackElement 23 Sep, 2023 @ 4:33pm 
I cant upload my addon to the workshop for some reason
BlackElement 22 Sep, 2023 @ 2:52pm 
How i make a custom Rocket Launcher swep?
Bisoso 9 Oct, 2020 @ 9:34am 
Is there a way to make thes weapons being used for npc ?
H3L1X4X1S 12 Sep, 2020 @ 3:53pm 
excuse me but i wanna make some halo sweps from the mcc (i use mcc) if you know which folder holds models, animations, ect plz tell me ;-;
< Laferyè > 9 Sep, 2020 @ 1:37pm 
can you tell us how to make a animated gun model
Fool 14 Apr, 2020 @ 3:10pm 
I don't want ironsights, I just want to use the basic hl2 base what is its name
Croatian 18 Dec, 2019 @ 4:32pm 
Carson you have to change SWEP.AdminOnly = false to SWEP.AdminOnly = true
ziph 21 Aug, 2019 @ 12:28pm 
I'm trying to make an admin gun have infinite ammo does anyone know how to?
Bloodbitt ΘΔ 12 Nov, 2018 @ 6:14am 
how do I make my folder into .gma? dragging and dropping into gmad isn't working
thanks
izaak☆ 24 Oct, 2018 @ 1:42am 
what do i do with the .lua files once i make them?