PrimaryAttack Help

Hey FP,
I am working on a SWEP and I’ve run into a complication. So I went to test a few things and I noticed that it wasn’t working exactly as I anticipated.

function SWEP:PrimaryAttack()
if CLIENT then chat.AddText( “CL_FIRE” ) end
if SERVER then print( “SV_FIRE” ) end
Only the “SV_FIRE” will print. If I were to run

SWEP.Test = 0

function SWEP:PrimaryAttack()
self.Test = self.Test + 1

if CLIENT then
hook.Add( ‘Tick’, ‘TestPrimaryAttack’, function()
chat.AddText( self.Test )
end )


I still only get '0’s, but the server value increments. Why wouldn’t PrimaryAttack be called on the clientside?

Is PrimaryAttack not shared? The wiki says it is
Am I doing something wrong?

  • Bump, been a day with no response.

I’ve tried a few things now with no luck. The SWEP contains AddCSLuaFile() ect.
Will SinglePlayer effect this? I know SteamID64() returns nil in singleplayer.

Have you tried running the AddText on self.Owner?

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 = 0
bullet.Force = self.Primary.Force
bullet.Damage = self.Primary.Damage
bullet.AmmoType = self.Primary.Ammo
local rnda = self.Primary.Recoil * -1
local rndb = self.Primary.Recoil * math.random(-1, 1)
self.Owner:FireBullets( bullet )
self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) )
self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
This maybe might help? I dont know why you are using print functions…

Thanks for the thought, but chat.AddText adds text to the LocalPlayer()'s chatbox. It is a clientside function and would only print the text to that clients chatbox.

EDIT: I am using print functions as an example here. Currently my issue is that PrimaryAttack isn’t happening on Clientside when it is a Shared hook.

Basically that is a test function so every-time I shoot the gun it should print “SV_FIRE” in console (which it does) and also print “CL_FIRE” in chat. (Which also appears in console as an orange-y colour in Single Player)

It seems to be a gmod bug, because it should print in both states.

SWEP:PrimaryAttack is only called on client if in multiplayer. The only reason it is called on client is for prediction.

You can however force it to be called on client:

if ( game.SinglePlayer() ) then self:CallOnClient( "PrimaryAttack" ) end

Note that in multiplayer, SWEP:PrimaryAttack is called multiple times per one “shot” with the gun, because prediction.

That clears a lot up. Thank you.

As for the multiple calls, this can be prevented by using IsFirstTimePredicted? Correct?

Read the link. Helped a lot. Thanks again.