Page 1 of 1

How to create a simple prop throwing weapon in Garry's Mod

#1 8Bit Bob  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 07-February 18

Posted 08 February 2018 - 10:10 AM

Hello and welcome! In this guide I will be showing you how to make a simple prop throwing weapon in Garry's Mod, and how to edit it to make it your own!

Getting Started
The first thing you'll need to do is create the folders where your .lua file will be stored. To do this go to C:\Program Files (x86)\Steam\steamapps\common\GarrysMod\garrysmod\addons and create a folder called "proplauncher". Inside of "proplauncher" create another folder called "lua", and then a folder inside of that called "weapons", you'll store our weapon file inside here. Now, go into Notepad++ (or whatever you're using) and make a file called "weapon_proplauncher.lua".

The Basic Info
Now, we'll start by adding the info that will be displayed when you hover over the weapon in your invenory. Replace these with whatever you want.
SWEP.PrintName = "Prop Launcher"
SWEP.Author = "( your name )"
SWEP.Instructions = "Left mouse to fire a chair!"


Great! Now we have to make it spawnable, and decide if it'll be restricted to admins.
SWEP.Spawnable = true
SWEP.AdminOnly = true


Alright, now we have to put in the clip info.
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"


And now we have to add some random info like slot position, weight, etc.
SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true


We are now finished with the general info! On to the models!

Models And Sounds
The first thing we have to do is add the world model and view model. The world model is what other players will see in your hand, while the view model is what you see when you're in first person. To find world/view models to use, go into Garry's Mod, open up the spawn menu, and go to spawnlist>browse>addons>games>Garry's Mod*>weapons. If the picture in the spawnlist is of just a weapon then it is a world model, but if it has hands holding the weapon it is a view model. Once you find one you like right-click it and hit "copy to clipboard" and the paste it in your code.

*You can also use weapons from other Source games such as CS:GO and TF2.
SWEP.ViewModel = "models/weapons/v_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"


Now we have to add the sound it makes when you shoot (this will be called later). To find a sound to use go into Garry's Mod, open up the spawn menu, then go to spawnlists>browse sounds>games>Garry's Mod*. Once you find one you like copy it to the clipboard and paste it into your code.

*You can also get sounds from other Source games.
local ShootSound = Sound( "Metal.SawbladeStick" )


Alrighty, now on to the actual prop throwing code!

The "Meat" Of The Code
Okay, now it's time to make this thing actually work. The first thing we have to do is call a function when the left mouse button is pressed.
function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 ) --This sets how fast it shoots the props.  It currently launches one prop every 1/2 a second.
	self:ThrowProp( "models/props_junk/wood_crate001a.mdl" ) --This is the model for the prop it shoots.  If you would like to change it copy any prop to the clipboard and paste it inside the quotation marks.

end


Now we have to call a function when the right mouse button is pressed.
function SWEP:SecondaryAttack()

	self:ThrowProp( "models/props_c17/FurnitureChair001a.mdl" )

end


Now we have to create a function that when called, causes the player to fire a prop! First we have to get some random information like the sound, model and such.
function SWEP:ThrowProp( model_file )

	self:EmitSound( ShootSound )
	if ( CLIENT ) then return end
	local ent = ents.Create( "prop_physics" )
	if ( !IsValid( ent ) ) then return end
	ent:SetModel( model_file )


Now we have to set the position and get the physics object.
	ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
	ent:SetAngles( self.Owner:EyeAngles() )
	ent:Spawn()

	local phys = ent:GetPhysicsObject()
	if ( !IsValid( phys ) ) then ent:Remove() return end


We then have to apply the force so the prop actually throws instead of just falling to the ground.
	local velocity = self.Owner:GetAimVector()
	velocity = velocity * 100 --You can edit this to make it faster or slower if you want.
	velocity = velocity + ( VectorRand() * 10 )
	phys:ApplyForceCenter( velocity )



And finally we have to add this entity to the cleanup and undo lists.
cleanup.Add( self.Owner, "props", ent )

	undo.Create( "Thrown_Prop" )
		undo.AddEntity( ent )
		undo.SetPlayer( self.Owner )
	undo.Finish()
end


To enable the launcher: you load into a map, open up the spawn menu (you do this by holding in the 'q' key) and go into weapons>Other you should be able see/equip the prop launcher.

Conclusion
And that should be it! You should now have a fully working Garry's Mod addon! You can edit some of the aspects of this weapon such as models, sounds, velocity, etc. and make your own unique take on it. I hope this was helpful, and thanks for reading!

-Bob

Is This A Good Question/Topic? 0
  • +

Replies To: How to create a simple prop throwing weapon in Garry's Mod

#2 AuditLPN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-February 18

Posted 19 February 2018 - 08:39 AM

Neat, i often found gmod a fun game to learn with, e2 is cool and if you like challenges you can try to defeat CAC, i made a lua executor so i can load before autorun and detour hooks.
Once that was done i just played around with net.messages until i got bored.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1