Restrict Firing of weapon to specific groups?

I have a custom weapon that’s working perfect but im trying to add some code that changes what happens when the weapon is fired by different ulx groups.

Here’s my broken group code:



function SWEP:PrimaryAttack()

	// This is the position your looking at
	local eyetrace = self.Owner:GetEyeTrace() 	

	self.BaseClass.ShootEffects (self);
	
	// The rest is only done on the server
	if (!SERVER) then return end
	
	// Make a npc
	if ply:CheckGroup( "group1" ) or ply:CheckGroup ( "group2" ) then
	
			local effectdata = EffectData()
		effectdata:SetOrigin( self.Owner:GetPos() )
		effectdata:SetNormal( self.Owner:GetPos() )
		effectdata:SetMagnitude( 16 )
		effectdata:SetScale( 1 )
		effectdata:SetRadius( 16 )
	util.Effect( "Sparks", effectdata )
	
	
		else
	
			local effectdata = EffectData()
		effectdata:SetOrigin( self.Owner:GetPos() )
		effectdata:SetNormal( self.Owner:GetPos() )
		effectdata:SetMagnitude( 8 )
		effectdata:SetScale( 1 )
		effectdata:SetRadius( 16 )
	util.Effect( "Sparks", effectdata )
	
		return
	end
	
			local ALSound = Sound( "weapons/rpg/rocketfire1.wav" )
			self.Weapon:EmitSound( ALSound )
	
			self:Remove()
end


Any idea what im doing wrong?

You’d definitely want most of the PrimaryAttack function to be shared ( such as audio, ammo, etc ) with IsFirstTimePredicted thrown into the mix too to allow proper prediction.

I don’t see where you define ply in the function. For SWEPs self.Owner would be a reference to the player currently holding the weapon.

I do have that shared, i just didn’t include it to highlight the code that doesnt work for me.

Nevermind, i changed to self.Owner for the group checks and now it works.

Thanks Acecool.

In regards to the code being shared, I was referring to this line:


if (!SERVER) then return end


Also, you’d want to define the Sound( ) outside of the function so that it doesn’t continuously pull it into memory ( like Material ):


			local ALSound = Sound( "weapons/rpg/rocketfire1.wav" )
			self.Weapon:EmitSound( ALSound )

If you call EmitSound on the server only, and not for the client using the gun then the client holding the gun will have a noticeable delay; the other clients won’t because everything they’re receiving is networked anyway.

Hopefully that provides a bit more insight.

If the thread questions have been solved, please mark the topic as solved by clicking “Mark As Solved” in the upper left-hand corner of the page.