Custom Muzzleflash Problem

Code:

//SWEP:FireAnimationEvent\
function SWEP:FireAnimationEvent( pos, ang, event, options )

if( event == 21 or event == 22) then

 local muzzle = EffectData();
 muzzle:SetOrigin( pos );
 muzzle:SetAngles( ang );
 muzzle:SetEntity( self );
 muzzle:SetAttachment( 1 );

 util.Effect( "AirboatMuzzleFlash", muzzle);
 
 return true;
 
 end

end
//SWEP:FireAnimationEvent\

//SWEP:PrimaryFire\
function SWEP:PrimaryAttack()

if ( !self:CanPrimaryAttack() ) then return end

local bullet = {} 
	bullet.Num = self.Primary.NumberofShots //The number of shots fired
	bullet.Src = self.Owner:GetShootPos() //Gets where the bullet comes from
	bullet.Dir = self.Owner:GetAimVector() //Gets where you're aiming
	bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0)
            //The above, sets how far the bullets spread from each other. 
	bullet.Tracer = 1 
	bullet.Force = self.Primary.Force 
	bullet.Damage = self.Primary.Damage 
	bullet.AmmoType = self.Primary.Ammo 
	bullet.TracerName = "AirboatGunTracer"
	bullet.Callback = function ( attacker, tr, dmginfo ) 
					local effect = EffectData();
					effect:SetOrigin(tr.HitPos);
					effect:SetNormal( tr.HitNormal );
					util.Effect("MetalSpark", effect);
					dmginfo:SetDamageType(DMG_AIRBOAT);		                
				  end;
				  


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:TakePrimaryAmmo(self.Primary.TakeAmmo) 

self.Owner:MuzzleFlash();
self.Owner:SetAnimation( PLAYER_ATTACK1 );
self:SendWeaponAnim( ACT_VM_PRIMARYATTACK );

self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
self:SetNextSecondaryFire( CurTime() + self.Primary.Delay )

end
//SWEP:PrimaryFire\

In ThirdPerson everything is ok but in first person the muzzleflash is showed as a third person (its not attached to View model Muzzle but for Third person).

PICS:

There are sometimes issues with MuzzleFlash if it isn’t initially set up in the SWEP a certain way…

I’d recommend, in the future, you to use


 or [lua] tags around the code; makes things easier to read.

CLIENTSIDE:


//
// Vars
//
SWEP.CSMuzzleFlashes = true;

//
// Fixes Double MuzzleFlash and other issues
//
function SWEP:FireAnimationEvent( _pos, _ang, _event, _options )
– Disables animation based muzzle _event
if ( _event == 20 ) then return true end

-- Disable thirdperson muzzle flash
if ( _event == 5001 ) then return true end

end




SHARED:


// Sometimes muzzle flashes can be temperamental. Make sure you use IsFirstTimePredicted, put the muzzle-flash util.Effect inside a timer.Simple 0 which calls it next frame
local _p = self.Owner;
local _effect = EffectData( );
_effect:SetOrigin( _p:GetShootPos( ) );
_effect:SetEntity( self );
_effect:SetStart( _p:GetShootPos( ) );
_effect:SetNormal( _p:GetAimVector( ) );
_effect:SetAttachment( 1 );

timer.Simple( 0, function( )
	if ( !self.Owner ) then return end
	if ( !IsFirstTimePredicted( ) ) then return; end
	util.Effect( "gunsmoke", _effect );
end );



Your effect may differ from "gunsmoke". AirboatMuzzleFlash for me does come from the muzzle in 3rd person, but in first person it appears to come from chest / crotch. The shared code should be in the swep base under MuzzleEffects or another helper function.