Garry's Mod

Garry's Mod

331 ratings
How to make a weapon/SWEP
By Mr Pops Alot
This guide will show you how to make your own SWEP
14
2
   
Award
Favorite
Favorited
Unfavorite
Materials
A .txt file editer that can edit .lua files
I recomend Notepad + + since its basicly a coding tool
the download link is here[notepad-plus-plus.org]

A 3D modeling program such as blender[www.blender.org](If your making your own models)
The Codeing
Since it may be your first time making a SWEP or even coding, we will start with a simple pistol

First, go to the drive where you instaled the game(Mine is C:) > Program Files(x86) > Steam > steamapps > common > GarrysMod > garrysmod > addons
and make a folder there
You can name it whatever you want as long it doesnt have any spaces on it
In the folder, make a new folder called lua, and inside that lua folder, add a new folder named weapons, now inside that weapons folder, make a text documument and change the file extention to .lua and rename it

Open the lua file, and type this
SWEP.PrintName = "weapon name" -- The name of the weapon SWEP.Author = "your name" SWEP.Contact = "your email adress"--Optional SWEP.Purpose = "add your purpose here" SWEP.Instructions = "add instructions here." SWEP.Category = "catorgory name" --This is required or else your weapon will be placed under "Other"

This will give us the name, the author(you), the purpose, the instructions, and what catagory will it be in

Then Type this
SWEP.Spawnable= true --Must be true SWEP.AdminOnly = false
These two lines will manage if we can spawn this and if ithis weapon is admin only

Then type
SWEP.Base = "weapon_base" local ShootSound = Sound("Weapon_Pistol.Single") SWEP.Primary.Damage = 5 --The amount of damage will the weapon do SWEP.Primary.TakeAmmo = 1 -- How much ammo will be taken per shot SWEP.Primary.ClipSize = 18 -- How much bullets are in the mag SWEP.Primary.Ammo = "Pistol" --The ammo type will it use SWEP.Primary.DefaultClip = 18 -- How much bullets preloaded when spawned SWEP.Primary.Spread = 0.1 -- The spread when shot SWEP.Primary.NumberofShots = 1 -- Number of bullets when shot SWEP.Primary.Automatic = false -- Is it automatic SWEP.Primary.Recoil = .2 -- The amount of recoil SWEP.Primary.Delay = 0.1 -- Delay before the next shot SWEP.Primary.Force = 100
This is the properties of the attack

Now for the secondery attack,
You must add this to your weapon, even though we wont be adding a secondery attack
SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none"


Now add this
SWEP.Slot = 2 SWEP.SlotPos = 1 SWEP.DrawCrosshair = true --Does it draw the crosshair SWEP.DrawAmmo = true SWEP.Weight = 5 --Priority when the weapon your currently holding drops SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false
Slot is what weapon hotbar will it be placed in
SlotPos is the place inside the hotbar

And this
SWEP.ViewModelFlip = false SWEP.ViewModelFOV = 60 SWEP.ViewModel = "models/weapons/c_pistol.mdl" SWEP.WorldModel = "models/weapons/w_pistol.mdl" SWEP.UseHands = true SWEP.HoldType = "Pistol" SWEP.FiresUnderwater = false SWEP.ReloadSound = "sound/epicreload.wav" SWEP.CSMuzzleFlashes = true

Initializeing code
function SWEP:Initialize() util.PrecacheSound(ShootSound) util.PrecacheSound(self.ReloadSound) self:SetWeaponHoldType( self.HoldType ) end

The primary attack
function SWEP:PrimaryAttack() if ( !self:CanPrimaryAttack() ) then return end local bullet = {} bullet.Num = self.Primary.NumberofShots bullet.Src = self.Owner:GetShootPos() bullet.Dir = self.Owner:GetAimVector() bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0) bullet.Tracer = 1 bullet.Force = self.Primary.Force bullet.Damage = self.Primary.Damage bullet.AmmoType = self.Primary.Ammo local rnda = self.Primary.Recoil * -1 local rndb = self.Primary.Recoil * math.random(-1, 1) self:ShootEffects() self.Owner:FireBullets( bullet ) self:EmitSound(ShootSound) self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) ) self:TakePrimaryAmmo(self.Primary.TakeAmmo) self:SetNextPrimaryFire( CurTime() + self.Primary.Delay ) end

Secondery attack
function SWEP:SecondaryAttack() end

Reloading
function SWEP:Reload() self:EmitSound(Sound(self.ReloadSound)) self.Weapon:DefaultReload( ACT_VM_RELOAD ); end
146 Comments
Mr. Blue Oct 28, 2024 @ 7:46pm 
how do i just add a reload sound?
Bald from Omizzers Oct 26, 2024 @ 10:47am 
Bullet spread i think
Bloodbitt ΘΔ Oct 5, 2024 @ 6:05pm 
what does SWEP.Spread measure?
redboi Aug 17, 2024 @ 5:41pm 
i get an unexpected symbol near '=' in the error menu and it cant be spawned in the spawnmenu
Yocp123 Aug 16, 2024 @ 2:37pm 
I am just trying to make the axe and sword from spookys jumpscare mansion but I still can't figure it out
Ducky1234 Jun 25, 2024 @ 12:36pm 
is it required admin false?
no Jun 25, 2024 @ 9:56am 
how do i change the model of the bullets?
Ducky1234 Jun 21, 2024 @ 7:02am 
is there a easier way?
Mei Jun 7, 2024 @ 5:52am 
protip: if you use linux, ~/.steam/steam/steamapps/common/GarrysMod/garrysmod/addons