First SWEP Help

Hey all, I am not sure if this is the way to go about it but…
[LUA]
AddCSLuaFile()

SWEP.Base = “weapon_base”
SWEP.PrintName = “Test SWEP”
SWEP.Author = “Keosan”
SWEP.Spawnable = false
SWEP.AdminSpawnable = true
SWEP.ViewModelFlip = false

SWEP.HoldType = “pistol”
SWEP.ViewModel = “models/weapons/w_alyx_gun_2.mdl”
SWEP.WorldModel = “models/weapons/w_alyx_gun_2.mdl”

SWEP.Primary.Recoil = 0.75
SWEP.Primary.Damage = 100
SWEP.Primary.NumShots = 1
SWEP.Primary.Spread = 0.05
SWEP.Primary.Cone = 0.05
SWEP.Primary.ClipSize = 10
SWEP.Primary.DefaultClip = 10
SWEP.Primary.Ammo = “pistol”
SWEP.Primary.Automatic = false
SWEP.Primary.Delay = 1

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

function SWEP:Initialize()
util.PrecacheSound(“newpistol/pistolshoot.wav”)
self:SetWeaponHoldType( self.HoldType )

end

function SWEP:PrimaryAttack()

if ( not self:CanPrimaryAttack() ) then

return

end

local Bullet {}
Bullet.Num = self.Primary.NumShots
Bullet.Src = self.Owner:GetShootPos()
Bullet.Dir = self.Owner:GetAimVector()
Bullet.Spread = Vector( self.Primary.Spread, self.Primary.Spread,0 )
Bullet.Tracer = 0
Bullet.Damage = self.Primary.Damage
Bullet.AmmoType = self.Primary.Ammo

self:ShootEffects()
self:FireBullets( Bullet)
self:EmitSound(“newpistol/pistolshoot.wav”)
self:TakePrimaryAmmo( 1 )
self:SetNextPrimaryFire( CurTime()+self.Primary.Delay)

end

function SWEP:SecondaryAttack()
if ( not self:CanSecondaryAttack() ) then

return

end

local bulletpos = self.Owner:GetBulletPos()
local die = ents.Create(“env_ar2explosion”)
die:SetPos( bulletpos.BulletPos )
die:SetOwner (self.Owner)
die:Spawn()
die:SetKeyValue(“iMagnitude”, “300”)
die:Fire(“Explode”, 0, 0)
die:EmitSound(“weapon_AWP.Single”, 400, 400)

  self:SetNextPrimaryFire( CurTime() + self.Secondary.Delay ) 
self:SetNextSecondaryFire( CurTime() + self.Secondary.Delay ) 

 end

end

function SWEP:Reload()
end

function SWEP:Think()
end
[/LUA]

I am trying to create a SWEP Pistol named test pistol which fires a bullet with primary button and then upon pressing the secondary button that bullet that was fired will expand to a given size and kill anything that falls within it. I am not sure of how to do this so i looked around and i couldn’t find anything so i need your help! What did i do right and what did i do wrong?

Bump

edit : I’d prefer if you not make my posts dumb if i legit need help and no one is responding. Thanks

[lua] local Bullet {} [/lua]

You need to define vars and tables like this:

[lua] local Bullet = {} [/lua]

Also, GetBulletPos is a nil func.

Okay thanks! So how would i go about retrieving the position of the bullet once its fired? Instead of creating a bullet do i do a Ent.Create and then track the entity??

Edit : I think i found a way… When firing the bullet all i have to do is find the bullets position in the world and create an entity AR2 explosion whatever the bullets position is at that moment when i press secondary button right?

I seem to be getting an error and my swep doesn’t show up in the menu!
[LUA]
SWEP.Base = “weapon_base”
SWEP.Author = “Keosan”
SWEP.Spawnable = false
SWEP.AdminSpawnable = true
SWEP.ViewModelFlip = false
SWEP.Category = “TEST ME”

SWEP.HoldType = “pistol”
SWEP.ViewModel = “models/weapons/w_alyx_gun_2.mdl”
SWEP.WorldModel = “models/weapons/w_alyx_gun_2.mdl”

SWEP.Primary.Recoil = 0.75
SWEP.Primary.Damage = 100
SWEP.Primary.NumShots = 1
SWEP.Primary.Spread = 0.05
SWEP.Primary.Cone = 0.05
SWEP.Primary.ClipSize = 10
SWEP.Primary.DefaultClip = 10
SWEP.Primary.Ammo = “pistol”
SWEP.Primary.Automatic = false
SWEP.Primary.Delay = 1

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

function SWEP:Initialize()
util.PrecacheSound(“sound/pistolshoot.wav”)
self:SetWeaponHoldType( self.HoldType )

end

function SWEP:PrimaryAttack()

if ( not self:CanPrimaryAttack() ) then

return

end

local Bullet = {}
Bullet.Num = self.Primary.NumShots
Bullet.Src = self.Owner:GetShootPos()
Bullet.Dir = self.Owner:GetAimVector()
Bullet.Spread = Vector( self.Primary.Spread, self.Primary.Spread,0 )
Bullet.Tracer = 0
Bullet.Damage = self.Primary.Damage
Bullet.AmmoType = self.Primary.Ammo

self:ShootEffects()
self:FireBullets( Bullet)
self:EmitSound(“newpistol/pistolshoot.wav”)
self:TakePrimaryAmmo( 1 )
self:SetNextPrimaryFire( CurTime()+self.Primary.Delay)

end

function SWEP:SecondaryAttack()
if ( not self:CanSecondaryAttack() ) then

return

end

local bulletpos = self.Bullet:Vector()
local die = ents.Create(“env_ar2explosion”)
die:GetPos(self.Owner.Bullet)
die:SetPos(self.Owner.Bullet)
die:SetOwner (self.Owner)
die:Spawn()
die:SetKeyValue(“iMagnitude”, “300”)
die:Fire(“Explode”, 0, 0)
die:EmitSound(“weapon_AWP.Single”, 400, 400)

  self:SetNextPrimaryFire( CurTime() + self.Secondary.Delay ) 
self:SetNextSecondaryFire( CurTime() + self.Secondary.Delay ) 

 end

function SWEP:Reload()
end

function SWEP:Think()
end
[/LUA]
[LUA]

[ERROR] addons/test swep/lua/weapons/weapon_test/shared.lua:87: ‘<eof>’ expected near ‘end’

  1. unknown - addons/test swep/lua/weapons/weapon_test/shared.lua:0
    [/LUA]

nvm

[editline]2nd August 2015[/editline]

I dont find the error, are you sure the code aint corrupt, also, clean up your code, example here:



SWEP.Base = "weapon_base"
SWEP.Author = "Keosan"
SWEP.Spawnable = false
SWEP.AdminSpawnable = true
SWEP.ViewModelFlip = false
SWEP.Category = "TEST ME"

SWEP.HoldType     = "pistol"
SWEP.ViewModel =  "models/weapons/w_alyx_gun_2.mdl"
SWEP.WorldModel = "models/weapons/w_alyx_gun_2.mdl"

SWEP.Primary.Recoil = 0.75
SWEP.Primary.Damage = 100
SWEP.Primary.NumShots = 1
SWEP.Primary.Spread = 0.05
SWEP.Primary.Cone = 0.05
SWEP.Primary.ClipSize = 10
SWEP.Primary.DefaultClip = 10
SWEP.Primary.Ammo =  "pistol"
SWEP.Primary.Automatic = false
SWEP.Primary.Delay = 1

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.Automatic =  false
SWEP.Secondary.Ammo = "none"

function SWEP:Initialize()
  util.PrecacheSound("sound/pistolshoot.wav")
  self:SetWeaponHoldType( self.HoldType )
end

function SWEP:PrimaryAttack()
  if ( not self:CanPrimaryAttack() ) then
    return
  end

  local Bullet = {}
  Bullet.Num = self.Primary.NumShots
  Bullet.Src = self.Owner:GetShootPos()
  Bullet.Dir = self.Owner:GetAimVector()
  Bullet.Spread = Vector( self.Primary.Spread, self.Primary.Spread,0 )
  Bullet.Tracer = 0
  Bullet.Damage = self.Primary.Damage
  Bullet.AmmoType = self.Primary.Ammo

  self:ShootEffects()
  self:FireBullets( Bullet)
  self:EmitSound("newpistol/pistolshoot.wav")
  self:TakePrimaryAmmo( 1 )
  self:SetNextPrimaryFire( CurTime()+self.Primary.Delay)
end

function SWEP:SecondaryAttack()
  if ( not self:CanSecondaryAttack() ) then
    return
  end

  local bulletpos = self.Bullet:Vector()
  local die = ents.Create("env_ar2explosion")
  die:GetPos(self.Owner.Bullet)
  die:SetPos(self.Owner.Bullet)
  die:SetOwner (self.Owner)
  die:Spawn()
  die:SetKeyValue("iMagnitude", "300")
  die:Fire("Explode", 0, 0)
  die:EmitSound("weapon_AWP.Single", 400, 400)
  self:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
	self:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
end

function SWEP:Reload()
end

function SWEP:Think()
end


[editline]2nd August 2015[/editline]

Also, do you test your gun in singleplayer?

I’ve been testing the code on my server so maybe i’ll try it single player. It says the error could be having unnecessary ends or possibly nill functions

Specify which server exactly, localy hosted? If local, then how, with SteamCMD, or by gmod itself?

Sorry. Nfoservers.com

the code you gave me got no line “87”, I dont know which OS the server is try, restarting it, and test it then.

The server OS is windows. However, i am not sure of what version. Maybe i have the wrong thing defined in SWEP.base? I am running DARKRP on my server. Just restarted it and nothing, the SWEP doesn’t even show in the Q menu! But at least this time i didn’t get any errors.

I was able to do /giveweapon <player> weapon_test however, the weapon is all messed up :confused:

The base is right, as you can see here: http://wiki.garrysmod.com/page/Structures/SWEP

[editline]2nd August 2015[/editline]

You’re using a WORLDMODEL as VIEWMODEL replace the “w_” in .ViewModel with a “v_”

[editline]2nd August 2015[/editline]

also, instead of SetWeaponHoldtype do SetHoldType.

Interesting. When i change the viewmodel to w_ i just get an error sign and when i change the self:SetHoldType(“pistol” i don’t see a gun at all(viewmodel wise).

Also, i just realized that when i press left click it doesn’t play my custom sound either. Heres a video of what the gun looked like before applying the above changes.

Edit : I also don’t know how to get the secondary function how i want it either. I wanted to have it where i’ll shoot the gun and press the secondary button and upon pressing it it will expand the bullet a certain radius and kill anything within that radius(similar to how an AR2 secondary hits you the body will just fade away into nothing) Does that mean i have to use an ent.create for the primary?

I am not that much into weapon scripting, but before you do primary & secondary functions, you should make the base functions, so it atleast gets drawn as a weapon, reffer to the wiki, they got the “chair throwing gun” you could copy it, and edit it.

I’ll do my best! In the mean time i’ll see if anyone else can chime in and help me :slight_smile: Thanks!

Daily bump