Trying to add Muzzleflash to Cmodel

Hello again Facepunch, I’m creating a SWEP and have ran into a bit of an issue.
I cannot seem to get a custom muzzle flash to load on to the cmodel of my SWEP.
[lua]function SWEP:CModel()
if (CLIENT) then
self.CModel = ClientsideModel(“models/weapons/c_models/c_gun.mdl”)
local vm = self.Owner:GetViewModel()
self.CModel:SetPos(vm:GetPos())
self.CModel:SetAngles(vm:GetAngles())
self.CModel:AddEffects(EF_BONEMERGE)
self.CModel:SetNoDraw(true)
self.CModel:SetParent(vm)
end[/lua]
In my PrimaryAttack function, I have
[lua]ParticleEffectAttach( “muzzle_flash”, PATTACH_POINT_FOLLOW , self.CModel, self.CModel:LookupAttachment( “muzzle” ))[/lua]
Now I already know that I have the attachment and particle set up properly. I’ve determined that the issue is that self.CModel is nil in the muzzle flash line even though it’s defined in another function globally.
Could it be something to do with self.CModel being defined in an “if (CLIENT)” statement? :s:
Any help / suggestions are appreciated. Please keep in mind that this is my first SWEP.

Bump? :s:

Try using

Entity:MuzzleFlash instead

[editline]15th August 2016[/editline]

Or

util.Effect - that’s more commonly used

Thank you for the suggestions. I’ve played around with util.Effect a bit but unfortunately, I can’t seem to get the result I want.

I believe that the solution of this issue would be to somehow allow “self.CModel” to be referenced outside of the “if (CLIENT)” statement.
For instance, even though “self.CModel” does exist,
[lua]function SWEP:CModel()
if (CLIENT) then
self.CModel = ClientsideModel(“models/weapons/c_models/c_gun.mdl”)
end[/lua]
when referenced in another function
[lua]ParticleEffectAttach( “muzzle_flash”, PATTACH_POINT_FOLLOW , self.CModel, self.CModel:LookupAttachment( “muzzle” ))[/lua]
it causes this error


attempt to index field 'CModel' (a nil value)

Bump

Im sure that error is being created in the server.
Why dont you try instead to put it in an “if CLIENT then” check, because it doesnt seem like.

Thanks for the response. I’m not sure if this is what you meant
[lua]if (CLIENT) then
ParticleEffectAttach( “muzzle_flash”, PATTACH_POINT_FOLLOW , self.CModel, self.CModel:LookupAttachment( “muzzle” ))
end[/lua]
If it is though, that doesn’t produce the particle nor does it create a lua error. (I am positive that I am calling the right particle, so that isn’t the issue.)

You can use util.Effect on the weapon’s first attachment. Example:


if ( IsFirstTimePredicted() ) then
		local pPlayer = pWeapon:GetOwner()
		
		if ( pPlayer == NULL ) then
			return true
		end
		
		local pViewModel = pPlayer:GetViewModel()
		
		if ( pViewModel == NULL ) then
			return true
		end
		
		local data = EffectData()
			data:SetEntity( pViewModel )
			data:SetAttachment(1)
			data:SetScale(1)
		util.Effect( "CS_MuzzleFlash", data )
	end
end

I appreciate the response but the issue with what you have suggested is that the c_arms of my weapon (not playermodel arms, think of it as a TF2 SWEP) has different attachments than the c_model applied to it, the muzzle attachment simply isn’t on the c_arms, it’s only on the c_model applied to it, and that’s really making this difficult. :frown:

Bump.

Bump. (x2) :dead:

You are most likely trying to attach a particle effect on the serverside to a clientside only model.

I probably am. :s:
How would I go about solving that? I believe I tried putting “if (CLIENT) then” around the particle, but then no error was produced and the particle wasn’t visible.

Start by reading http://wiki.garrysmod.com/page/Prediction