Charge Weapon Issue

The swep has no errors but the bullets dont fire even though for the debugging it says they are. Also the hud doesn’t update the SWEP.Charge

[lua]SWEP.Author = “Atwal”
SWEP.Contact = “Facepunch”
SWEP.Purpose = “Weapon for all situations”
SWEP.Instructions = “Primary to fire laser, Secondary to fire a rocket”
SWEP.Category = “Atwal’s SWEPs”

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_irifle.mdl”
SWEP.WorldModel = “models/weapons/w_irifle.mdl”

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”;

SWEP.Sound = Sound (“weapon_ar2.Single”)
SWEP.Damage = 1
SWEP.Spread = 0
SWEP.NumBul = 1
SWEP.Delay = 0.1
SWEP.Force = 5
SWEP.Charge = 100

function SWEP:Deploy()
return true
end

function SWEP:Holster()
return true
end

function SWEP:Think()

end

function SWEP:PrimaryAttack() --when +attack1 happens

if self.Charge > 0 then 

local bullet = {} --creates a table for the properties of the bullet
bullet.Num 		= self.NumBul --number of bullets you are shooting
bullet.Src 		= self.Owner:GetShootPos() -- Source, where you are standing
bullet.Dir 		= self.Owner:GetAimVector() -- direction of bullet, where you are looking
bullet.Spread 	= Vector( self.Spread, self.Spread, 0 ) -- spread of bullet, how accurate it is
bullet.Tracer	= 0	-- this doesn't really affect anything
bullet.Force	= self.Force -- how powerful it is
bullet.Damage	= self.Damage --how much damage it does to people
bullet.AmmoType = self.Primary.Ammo --what type of ammo you are using

self.Owner:FireBullets( bullet ) --actually shoots the bullet.

Msg("Bullet Fired
")
self.Weapon:EmitSound ( self.Sound )
– this makes the sound, which I specified earlier in the code

--this makes the shooting animation for the 357 for view model and world model

self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
--this sets the delay for the next primary and secondary fires.

self.Charge = self.Charge - 1 --removes 1 ammo from our clip

Msg("Charge Lowered
")
end
end --end our function

function SWEP:SecondaryAttack()
if self.Charge < 100 then
self.Charge = self.Charge + 1
Msg("Charge + 1
")
end
end

function SWEP:DrawHUD()
draw.SimpleText(“Energy” … self.Charge … “%” ,“ScoreboardText”,500,500,Color(255,0,0,255))
end[/lua]

Your bullet issue: isn’t it supposed to be self.Weapon:FireBullets and not self.Owner?

Your HUD issue: make sure you set the x/y alignment arguments in case (http://wiki.garrysmod.com/?title=Draw.SimpleText)

Thank you so much, do you know how to make the Charge recharge when the user isnt firing?

Also the drawing doesnt work.

You could do something with a timer if that’s what you wanted. It would run 100 times with whatever delay you wanted.

[lua]function SWEP:PrimaryAttack()
timer.Create(“Charger”,0.02,100,function() self.Charge = self.Charge + 1 end)
end[/lua]

As for the drawing try this:
[lua]function SWEP:PrimaryAttack()
timer.Create(“Charger”,0.02,100,function() self.Charge = self.Charge + 1 end)
end[/lua]

If it doesn’t work, then that’s a problem with the DrawHUD hook and not the code itself.

As for the drawing try this:
[lua]function SWEP:PrimaryAttack()
timer.Create(“Charger”,0.02,100,function() self.Charge = self.Charge + 1 end)
end[/lua]

you sure thats what you meant to post :stuck_out_tongue:

Awesome its work but when i load it up im getting lua spam only at the beginning

Timer Error: weapons\m.a.r.2\shared.lua:31: attempt to index global ‘self’ (a nil value)

Oops. I mean to put in a HUD test thing. So the HUD works now?

Also, what’s line 31?

Ahh sorry for the late reply was camping
i fixed the error on line 31
Here is the code so far

instead of directly getting the charge im going to try to assign a variable to it
also the effect isnt working
Drawhud is being called but not updating

[lua]
SWEP.Author = “Atwal”
SWEP.Contact = “Facepunch”
SWEP.Purpose = “Weapon for all situations”
SWEP.Instructions = “Primary to fire laser, Secondary to fire a rocket”
SWEP.Category = “Atwal’s SWEPs”

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_irifle.mdl”
SWEP.WorldModel = “models/weapons/w_irifle.mdl”

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”;

SWEP.Sound = Sound (“weapon_ar2.Single”)
SWEP.Damage = 1
SWEP.Spread = 0
SWEP.NumBul = 1
SWEP.Delay = 0
SWEP.Force = 5
SWEP.Charge = 100

function SWEP:Deploy()
return true
end

function SWEP:Holster()
return true
end

function SWEP:Think()

end

function SWEP:PrimaryAttack() --when +attack1 happens

timer.Create("Charger",0.5,100,function() self.Charge = self.Charge + 1 end)  

if self.Charge &gt; 0 then 

local bullet = {} --creates a table for the properties of the bullet
bullet.Num 		= self.NumBul --number of bullets you are shooting
bullet.Src 		= self.Owner:GetShootPos() -- Source, where you are standing
bullet.Dir 		= self.Owner:GetAimVector() -- direction of bullet, where you are looking
bullet.Spread 	= Vector( self.Spread, self.Spread, 0 ) -- spread of bullet, how accurate it is
bullet.Tracer	= 0	-- this doesn't really affect anything
bullet.Force	= self.Force -- how powerful it is
bullet.Damage	= self.Damage --how much damage it does to people

self.Weapon:FireBullets( bullet ) --actually shoots the bullet.	

local sPoint = self.Owner:GetShootPos()
local ePoint = self.Owner:GetAimVector()

local effectdata = EffectData()
effectdata:SetStart( sPoint ) – not sure if we need a start and origin (endpoint) for this effect, but whatever
effectdata:SetOrigin( ePoint )
effectdata:SetScale( 1 )
util.Effect( “AR2Impact”, effectdata )

self.Weapon:EmitSound ( self.Sound )
-- this makes the sound, which I specified earlier in the code

--this makes the shooting animation for the 357 for view model and world model

self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
--this sets the delay for the next primary and secondary fires.

self.Charge = self.Charge - 1 --removes 1 ammo from our clip

end

end --end our function

function SWEP:SecondaryAttack()
if self.Charge < 100 then
self.Charge = self.Charge + 1

end
end

function SWEP:DrawHUD()
print( “DrawHUD has been called!” )
draw.SimpleText("Charge " … self.Charge … “%”, “ScoreboardText”, ScrW() / 2 - 70, ScrH() - 63, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end[/lua]

I’m not that great with SWEPs or effects, but you could try this for the HUD…


function SWEP:Think() SWEP:DrawHUD() end

Like that maybe?

As for the effect… use the Super_Explosion effect to make it more evident as to whether it’s actually working.

Also, are you trying to make an effect show up at the end of the player’s eye trace or what?

If so, try this…

[lua]local ed = EffectData()
local tr = self.Owner:GetEyeTrace()
ed:SetOrigin(tr.HitPos)
ed:SetNormal(tr.HitNormal)
ed:SetAttachment(tr.PhysicsBone)
ed:SetEntity(tr.Entity)
util.Effect( “super_explosion”,ed)[/lua]

God damnit i dont have a clue why its not working i even put it in the clientside lua file -snip-

Cl_init.lua
[lua]function SWEP:DrawHUD()
local charge = self.Charge
draw.SimpleText("Charge " …charge… “%”, “ScoreboardText”, ScrW() / 2 - 70, ScrH() - 63, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end[/lua]

shared.lua
[lua]SWEP.Author = “Atwal”
SWEP.Contact = “Facepunch”
SWEP.Purpose = “Weapon for all situations”
SWEP.Instructions = “Primary to fire laser, Secondary to fire a rocket”
SWEP.Category = “Atwal’s SWEPs”

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_irifle.mdl”
SWEP.WorldModel = “models/weapons/w_irifle.mdl”

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”;

SWEP.Sound = Sound (“weapon_ar2.Single”)
SWEP.Damage = 1
SWEP.Spread = 0
SWEP.NumBul = 1
SWEP.Delay = 0
SWEP.Force = 5
SWEP.Charge = 100

function SWEP:Deploy()
return true
end

function SWEP:Holster()
return true
end

function SWEP:Think()
end

function SWEP:PrimaryAttack( ) --when +attack1 happen
if self.Owner:Alive() then
timer.Create(“Charger”,0.3,100,function() self.Charge = self.Charge + 1 end)
end
if self.Charge > 0 then

local bullet = {} --creates a table for the properties of the bullet
bullet.Num 		= self.NumBul --number of bullets you are shooting
bullet.Src 		= self.Owner:GetShootPos() -- Source, where you are standing
bullet.Dir 		= self.Owner:GetAimVector() -- direction of bullet, where you are looking
bullet.Spread 	= Vector( self.Spread, self.Spread, 0 ) -- spread of bullet, how accurate it is
bullet.Tracer	= 0	-- this doesn't really affect anything
bullet.Force	= self.Force -- how powerful it is
bullet.Damage	= self.Damage --how much damage it does to people

self.Weapon:FireBullets( bullet ) --actually shoots the bullet.	

local sPoint = self.Owner:GetShootPos()
local ePoint = self.Owner:GetAimVector()

local ed = EffectData()
local tr = self.Owner:GetEyeTrace()
ed:SetOrigin(tr.HitPos)
ed:SetNormal(tr.HitNormal)
ed:SetAttachment(tr.PhysicsBone)
ed:SetEntity(tr.Entity)
util.Effect( “super_explosion”,ed)
self.Weapon:EmitSound ( self.Sound )
– this makes the sound, which I specified earlier in the code

--this makes the shooting animation for the 357 for view model and world model

self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
--this sets the delay for the next primary and secondary fires.

self.Charge = self.Charge - 1 --removes 1 ammo from our clip

end

end --end our function

function SWEP:SecondaryAttack()
if self.Charge < 100 then
self.Charge = self.Charge + 1

end
end[/lua]

thanks for all the help by the way :slight_smile: