Help with a Lua SWep.

Hey, i’m very new to coding and Lua. I have started a pistol SWep to see what to do, some of it works and some does not and I don’t really know which parts. If anyone could help clean this up and fix the errors that would be great! here’s what I have in my ‘shared.lua’:



/*--------------credits----------------
SWeps by OrangeOz and Lord Giacomo
Based on swep from buu342s Swep Creator
-------------------------------------*/

/*----to do----
raise/lower gun
picture
optimize
rebalance
--------------*/

//info:
SWEP.PrintName = "Pistol"
    
SWEP.Author = "OrangeOz"
SWEP.Purpose = "This is the basic pistol, nothing too fancy."
SWEP.Instructions = "Left click to shoot."

SWEP.Category = "Roleplay CS:S Weapons V1"

SWEP.Spawnable= true
SWEP.AdminSpawnable= true
SWEP.AdminOnly = false

//models:
SWEP.ViewModelFOV = 50
SWEP.ViewModel = "models/weapons/cstrike/c_pist_fiveseven.mdl" 
SWEP.WorldModel = "models/weapons/w_pist_fiveseven.mdl"
SWEP.ViewModelFlip = false

SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.Slot = 1
SWEP.SlotPos = 1

SWEP.UseHands = true

SWEP.HoldType = "Pistol" 

SWEP.FiresUnderwater = false

SWEP.DrawCrosshair = true

SWEP.DrawAmmo = true

SWEP.Base = "weapon_base"

SWEP.ReloadSound = "weapons/fiveseven"

//bullet and damage info:
SWEP.Primary.Sound = Sound("Weapon_FiveSeven.Single") 
SWEP.Primary.Damage = 20
SWEP.Primary.TakeAmmo = 1
SWEP.Primary.ClipSize = 12 
SWEP.Primary.Ammo = "Pistol"
SWEP.Primary.DefaultClip = 12
SWEP.Primary.Spread = 0.2
SWEP.Primary.NumberofShots = 1
SWEP.Primary.Automatic = false
SWEP.Primary.Recoil = 0.5
SWEP.Primary.Delay = 0.15
SWEP.Primary.Force = 10
SWEP.IconLetter = "u"
//SWEP.WepSelectIcon = ""


SWEP.Secondary.ClipSize = 0
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.CSMuzzleFlashes = true

killicon.AddFont("weapon_rpfiveseven", "CSKillIcons", SWEP.IconLetter, Color(255, 80, 0, 255))

surface.CreateFont("CSKillIcons", {font = "csd", size = ScreenScale(30), weight = 500, antialias = true, additive = true})

function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)

	draw.SimpleText(self.IconLetter, "CSSelectIcons", x + wide / 2, y + tall * 0.2, Color(255, 210, 0, 255), TEXT_ALIGN_CENTER)
end


//weapon movement speed:
local previousSpeed
local previousrunSpeed

function SWEP:Deploy()
    previousSpeed = self:GetOwner():GetWalkSpeed()
	previousrunSpeed = self:GetOwner():GetRunSpeed()
    self:GetOwner():SetWalkSpeed(previousSpeed*1)
	self:GetOwner():SetRunSpeed(previousrunSpeed*1)
    return true
end

function SWEP:Holster(wep)
    self:GetOwner():SetWalkSpeed(previousSpeed)
	self:GetOwner():SetRunSpeed(previousrunSpeed)
    return true
end

function SWEP:Initialize()
util.PrecacheSound(self.Primary.Sound) 
        self:SetWeaponHoldType( self.HoldType )
self.m_WeaponDeploySpeed = tonumber( 1 )

end 

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 

//recoil settings
ply:SetEyeAngles(ply:EyeAngles() + Angle(-1,0,0))
local rnda = self.Primary.Recoil * -1 
local rndb = self.Primary.Recoil * math.random(-1, 1) 
 
self:ShootEffects()
 
self.Owner:FireBullets( bullet ) 
self:EmitSound(Sound(self.Primary.Sound)) 
self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) ) 
self:TakePrimaryAmmo(self.Primary.TakeAmmo) 
 
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
self:SetNextSecondaryFire( CurTime() + self.Primary.Delay ) 
end 

function SWEP:SecondaryAttack()
end

function SWEP:Reload()
self:EmitSound(Sound(self.ReloadSound)) 
        self.Weapon:DefaultReload( ACT_VM_RELOAD );
end


Thanks.

What error are you even having?

wow really, you directly send us the code and want to improve it? we dont even know what you need.

Correct me if i am wrong, but doesn’t local variables have to be within a function to work?

No they don’t. They can be written anywhere, however I believe if they’re written in a function they’ll only work in that function. If they’re written outside they’ll work anywhere in the file.

Hmm, so the only difference between a global variable and a local variable is that global cannot be returned nil correct?

I think a global variable can be accessed anywhere.

Example :

y = 6

would be able to be accessed in another file which is why you should use globals at your own risk

while locals are like

local y = 6

and they’re only accessible within that file causing no overrides

Sorry, I said I was new so I really don’t even know whats wrong, I think it’s the bit with killicons and select icons, thats really all I know.

[editline]9th August 2015[/editline]

Like, what else do I do? I don’t even know what’s broken or why.

Sorry if i’m being an ignorant noob swep maker, I have solved some some errors but now i’m getting an error when I try to change the weapon hold type. I think it’s something to do with the default weapon_base.

Here’s the error in console:




[ERROR] gamemodes/base/entities/weapons/weapon_base/sh_anim.lua:31: bad argument #1 to 'lower' (string expected, got nil)
  1. lower - [C]:-1
   2. SetWeaponHoldType - gamemodes/base/entities/weapons/weapon_base/sh_anim.lua:31
    3. unknown - lua/weapons/weapon_rpfiveseven/shared.lua:114


If anyone finds themselves here in the distant future, I fixed the weapon hold types by changing



SWEP:SetWeaponHoldType( self.HoldType )


to



self:SetWeaponHoldType("revolver")


You’re welcome.