No errors; Weapon continues to fire.(Jammable Weapons)
3 replies, posted
I have an error with some old code I had found from garrysmod.org, it also seems to come from a youtube video as well, on weapon jamming.
Basically as you use a weapon a random number is given and if that random number is given then the weapon is supposed to stop firing.
And it all seemed to work out well at first, the weapon would stop and the word jammed appears on the screen. But sometimes when the
weapon is supposed to be jammed, it will display the word jammed like usual but then continue to fire and NOT take any bullets out of the current magazine.
Kinda like as if it had infinite ammo or something along the lines of that. Sometimes it won't actually fire a bullet but the weapon will lost ammo upon holding down
the left mouse button for primary fire.
Here is the code:
[CODE]--mind you this is within a shared.lua file within the folder of weapon_base
--multiple weapons will be using the jam feature
SWEP.Jam = {}
SWEP.Jam.MaxProb = 60 -- A number between 0 and this will be picked each shot. If the number is this, the gun jams. You must reload to solve.
SWEP.Jam.Jammed = false
SWEP.Jam.Sound = Sound("Weapon_Pistol.Empty")
function SWEP:Initialize()
if ( SERVER ) then
self:SetWeaponHoldType( self.HoldType )
end
self.Weapon:SetNetworkedBool( "Jammed", false )
end
function SWEP:Reload()
self.Weapon:DefaultReload( ACT_VM_RELOAD );
self.Jam.Jammed = false
self.Weapon:SetNetworkedBool( "Jammed", false )
end
function SWEP:PrimaryAttack()
self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
if ( !self:CanPrimaryAttack() ) then return end
local jamnum = math.random(0,self.Jam.MaxProb)
if ( jamnum == self.Jam.MaxProb ) then -- The magic jamming number
self.Jam.Jammed = true
self.Weapon:SetNetworkedBool( "Jammed", true )
end
if ( not self.Jam.Jammed ) then
self.Weapon:EmitSound( self.Primary.Sound )
self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone ) --this function is later done in the weapon, just didn't wanna post too much
self:TakePrimaryAmmo( 1 )
self.Owner:ViewPunch( Angle( math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0 ) )
return true
else
self.Weapon:EmitSound(self.Jam.Sound)
return false
end
end[/CODE]
Now as a side-note this is NOT my code, I am just repairing it along with an old gamemode I found on garrysmod.org to practice.
Not sure but maybe I have to use the SWEP:Think() to make sure it doesn't keep firing?
Any information on how I could fix this or what I'm doing wrong would be very helpful.
SWEP:PrimaryAttack() is called on server and client.
[LUA]
local jamnum = math.random(0,self.Jam.MaxProb)
[/LUA]
This line is probably going to give a different result on server and client, thus creating a mismatch between server and client.
Now I am pretty sure you aren't really shooting bullets, it just looks like that for your client.
You should test that just to make sure.
I know this is a bit of a late reply, but I've been doing testing trying to get a value for just the serverside or clientside to stop the gun from firing on both sides at the same time but I've got no idea what I've been
doing wrong or how to fix this.
Is there a way to set it up on the serverside to get a number, then make it the same on the client side?
Any suggestions for fixing this?
[QUOTE=Apple_Bloom;46150753]I know this is a bit of a late reply, but I've been doing testing trying to get a value for just the serverside or clientside to stop the gun from firing on both sides at the same time but I've got no idea what I've been
doing wrong or how to fix this.
Is there a way to set it up on the serverside to get a number, then make it the same on the client side?
Any suggestions for fixing this?[/QUOTE]
Use the [url=http://wiki.garrysmod.com/page/Net_Library_Usage]net library[/url]
Sorry, you need to Log In to post a reply to this thread.