Swep Problem

Hi, I just finished a simple SWep. I tried it out and it worked. Then i wanted to add a function that, when you press Primary/Secondary Fire + E (Use), switches between automatic firing for Primary/Secondary Fire. I wrote the Code and tried it out ingame. Shooting works, but when i press E + Primary/Secondary, it doesnt toggle between Automatic/non-Automatic firing. You could only hear the switching sound.

here is the code of the function:

Primary:
[lua]function SWEP:PrimaryAttack()
if ( !self:CanPrimaryAttack() ) then return end

//SWEP Mode
if self.Owner:KeyDown(IN_USE) then
if self.Primary.Automatic == false then
self.Primary.Automatic = true
self.Weapon:EmitSound(“weapons/smg1/switch_burst.wav”)
else
self.Primary.Automatic = false
self.Weapon:EmitSound(“weapons/smg1/switch_single.wav”)
end
end[/lua]

Secondary:
[lua]function SWEP:SecondaryAttack()
if ( !self:CanPrimaryAttack() ) then return end

//SWEP Mode
if self.Owner:KeyDown(IN_USE) then
if self.Secondary.Automatic == false then
self.Secondary.Automatic = true
self.Weapon:EmitSound(“weapons/smg1/switch_burst.wav”)
else
self.Secondary.Automatic = false
self.Weapon:EmitSound(“weapons/smg1/switch_single.wav”)
end
end[/lua]

Is there something wrong?

What is your problem exactly? The code you provided seems to be bugless (besides the un-ended functions…).

When i press E + Primary/Secondary, it does not change the Firemode!

Edit:
Un-ended functions? What do you mean?

It looks like css realsitic base v4’s shitty way of changing fire modes. Since the fire mode isnt set on the client it will act REALLY strangly.

Other than that, it seems ok, Apart from the missing end…

PrimaryAttack and SecondaryAttack are shared. They run on both the server and the client. There’s nothing wrong with the method of firemode change being used here.

i added one extra end:
[lua] if self.Owner:KeyDown(IN_USE) then
if self.Primary.Automatic == false then
self.Primary.Automatic = true
self.Weapon:EmitSound(“weapons/smg1/switch_burst.wav”)
else
self.Primary.Automatic = false
self.Weapon:EmitSound(“weapons/smg1/switch_single.wav”)
end
end
end

[/lua]

and the following happened as i ran the script:



] lua_openscript "weapons/bestgun2/shared.lua"
Running script weapons/bestgun2/shared.lua...
[lua\weapons\bestgun2\shared.lua:166] '<eof>' expected near 'end'

Edit:

Oh and i noticed, when i press E + Primary/Secondary it fires 2 times… seems like its not toggling…

You misspelled end…

Sorry, i didnt copy it right.

My guess at what happens;

  1. You hold E and press the fire button
  2. The scripts changes to automatic firemode
  3. Since it’s now automatic it will call :PrimaryAttack again some (usually very short) time later
  4. It changes back to semi automatic
  5. Since it’s again semi-automatic it doesn’t call :PrimaryAttack any more

Solution: change
[lua]
if self.Owner:KeyDown(IN_USE) then
[/lua]
to
[lua]
if self.Owner:KeyDown(IN_USE) and !self.Owner:KeyDownLast(IN_ATTACK) then --IN_ATTACK2 for secondary
[/lua]

:ms:

Cool it works! many Thanks!!!