In this tutorial we will create a weapon that fires chairs.
- Open your garry's mod folder.
- Open the 'addons' folder.
- Create a folder called 'chairlauncher'
- Create a folder inside called 'lua'
- Create a folder inside called 'weapons'
- Create a file called 'weapon_chairgun.lua'
Your folder structure should be like this:
You have created an addon called chairlauncher which contains a weapon called weapon_chairgun. Whenever you create a new weapon, most people use the weapon_ prefix, since most of the default weapons names also start with weapon_.
Add these to your weapon after you understand them.
This code is just informational. It shows up in the spawnmenu and the weapon selection menu.. so it will help people out if your weapon is complicated to use. You can also put your name in the author field so people know who made it.
SWEP.PrintName = "Chair Thrower"
SWEP.Author = "(your name)"
SWEP.Instructions = "Left mouse to fire a chair!"
Defines whether players can spawn this weapon from the spawnmenu.. and whether only admins can spawn it.
SWEP.Spawnable = true
SWEP.AdminOnly = true
Our weapon doesn't use any ammo or clips, so we just set the clip sizes to -1 and the ammo to "none".
We set primary to be automatic. This means that the player doesn't have to release the mouse button and press it again - it will continually fire.
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
These are mostly self explanatory. The higher the weight the more likely you are to switch to it. Slot and SlotPos decide where in your weapon menu the weapon will be.
SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true
The view-model and the world-model to use.
SWEP.ViewModel = "models/weapons/v_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
You can precache sounds by defining them as locals using the Sound function. This prevents the sound loading when you first shoot the weapon - causing a hitch.
ShootSound = Sound
The actual chair throwing code
Commented for your learning pleasure
( model_file )
( ShootSound )
( CLIENT ) then return end
ent = ents
( ent ) ) then return end
( model_file )
() * 16) )
phys = ent
( phys ) ) then ent
() return end
velocity = self
velocity * 100
(VectorRand() * 10)
( velocity )
( self.Owner, "props"
, ent )
( ent )
( self.Owner )
Can you edit the new weapon to:
- Use a different firing sound for left/right click
- Throw a melon when you press reload? (tip: WEAPON:Reload)
- Throw 3 chairs at a time