Gun with infinite ammo but not infinite clip, reloads auto if player is on ground?

How might I go about making this? I’m trying to make a flying weapon that can fly for a limited time (ammo clip), and then once you use up the flight time, you cannot “reload” your “clip” until you touch the ground, then it automatically reloads your clip (you have infinite ammo, you can do this infinitely) and you can fly again whenever you next go into the air.

Credit will be given of course.

This is a simple way to do it. (Untested)

[lua]
SWEP.Primary.ClipSize = 100

function SWEP:Reload( )

-- Cannot reload unless OnGround is true
if ( !self.Owner:OnGround( ) ) then return false; end

-- Gives the player exactly how many bullets he fired before reloading (Inf. Ammo)
self.Owner:GiveAmmo( self.Primary.ClipSize, "Pistol" )

end

function SWEP:PrimaryAttack( )

-- Can't fire if the clip is empty
if ( self.Weapon:Clip1( ) < 1 ) then return; end

-- How fast the player will be pushed forward
local vel = 1024

self.Owner:SetVelocity( self.Owner:GetAimVector( ) * vel )

self.Weapon:TakePrimaryAmmo( 1 )

end
[/lua]

Thanks very much for the help, I’m much nearer completion, but -

weapons/fly/shared.lua:106: attempt to call method ‘GiveAmmo’ (a nil value)

And you can only seem to jump up and fly for a while once, then you can’t ever again after that.

this should be in requests as you dont really want to work for it you just want other people to do it for you clearly

I can see why you would think that, but I actually had the weapon fully prepared except for limiting the flight. I’m a newbie to lua, and I’m trying to teach myself it by making these sorts of things.

You really need to search for stuff like this on the wiki your self its only way to learn how to use it well

That error is what causes the weapon to only work once. Make sure you are only calling GiveAmmo on the server as it is not available on the client. (Meaning that the error message should be orange when printed to your console.)

Read more about the function, here -> http://wiki.garrysmod.com/?title=Giveammo

HINT: Use the following line anywhere before the GiveAmmo line in your code to fix it.


if ( !SERVER ) then return; end

If this doesn-t work paste all your code and I will do my best to debug it for you.

-snip-

-snip snip snippety-

It doesn’t seem to work.
After enabling ammo display, it shows the ammo does indeed deplete whenever the player is flying, but after a certain amount of time it depletes to zero, but reloads despite being in the air. After 3 reloads or so, it stops reloading entirely (even when on the ground) and the player cannot fly at all despite there being more ammo to reload into the clip.

Thanks so much for the help, I’m sure you have better things to do than helping a lua newbie finish an SWEP;

[lua]AddCSLuaFile( “shared.lua” )

SWEP.Author = “Eagle with Garry’s laser dance SWEP and Zcom’s lua help.”
SWEP.Contact = “”
SWEP.Purpose = “”
SWEP.Instructions = “Left mouse flies, right click performs a stormy swipe which can send objects flying.”

if ( CLIENT ) then

SWEP.PrintName = “X” // ‘Nice’ Weapon name (Shown on HUD)
SWEP.Slot = 0 // Slot in the weapon selection menu
SWEP.SlotPos = 10 // Position in the slot
SWEP.DrawAmmo = false // Should draw the default HL2 ammo counter
SWEP.DrawCrosshair = false // Should draw the default crosshair
SWEP.DrawWeaponInfoBox = false // Should draw the weapon info box
SWEP.BounceWeaponIcon = false // Should the weapon icon bounce?
SWEP.SwayScale = 1.0 // The scale of the viewmodel sway
SWEP.BobScale = 1.0 // The scale of the viewmodel bob
SWEP.WepSelectIcon = surface.GetTextureID( “weapons/swep” )
SWEP.HoldType = “crowbar”

end

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.ViewModel = “”
SWEP.WorldModel = “”
SWEP.AnimPrefix = “python”

SWEP.Spawnable = false
SWEP.AdminSpawnable = false

SWEP.Primary.ClipSize = 1000 // Size of a clip
SWEP.Primary.DefaultClip = 1000 // Default number of bullets in a clip
SWEP.Primary.Automatic = true // Automatic/Semi Auto
SWEP.Primary.Ammo = “pistol”
SWEP.Primary.TakeAmmo = 1

SWEP.Secondary.ClipSize = -1 // Size of a clip
SWEP.Secondary.DefaultClip = -1 // Default number of bullets in a clip
SWEP.Secondary.Automatic = true // Automatic/Semi Auto
SWEP.Secondary.Ammo = “none”

/---------------------------------------------------------
Name: SWEP:PrimaryAttack( )
Desc: +attack1 has been pressed
---------------------------------------------------------
/
function SWEP:Think()
end

function SWEP:PrimaryAttack( )
– Can’t fire if the clip is empty
if ( self.Weapon:Clip1( ) < 1 ) then return; end

 -- How fast the player will be pushed forward  
 local vel = 20 

 self.Owner:SetVelocity( self.Owner:GetAimVector( ) * vel )  

 self.Weapon:TakePrimaryAmmo( 1 )  

end

/---------------------------------------------------------
Name: SWEP:SecondaryAttack( )
Desc: +attack2 has been pressed
---------------------------------------------------------
/
function SWEP:SecondaryAttack()
self.Weapon:SetNextSecondaryFire(CurTime() + .4)

local trace = self.Owner:GetEyeTrace()

if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
bullet = {}
bullet.Num = 1
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector(0, 0, 0)
bullet.Tracer = 0
bullet.Force = 700
bullet.Damage = 100
self.Owner:FireBullets(bullet)
self.Owner:SetAnimation( PLAYER_ATTACK1 );
self.Weapon:EmitSound(“physics/flesh/flesh_impact_bullet” … math.random( 3, 5 ) … “.wav”)
else
self.Weapon:EmitSound(“weapons/iceaxe/iceaxe_swing1.wav”)
self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
end

end

function SWEP:Reload( )

if ( !SERVER ) then return; end
-- Cannot reload unless OnGround is true  
 if ( !self.Owner:OnGround( ) ) then return false; end  

-- Gives the player exactly how many bullets he fired before reloading (Inf. Ammo)  
 self.Owner:GiveAmmo( self.Primary.ClipSize, "Pistol" )  

end

/---------------------------------------------------------
Name: SWEP:ShootBullet( )
Desc: A convenience function to shoot bullets
---------------------------------------------------------
/
function SWEP:ShootEffects()

self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		// View model animation
self.Owner:MuzzleFlash()								// Crappy muzzle light
self.Owner:SetAnimation( PLAYER_ATTACK1 )				// 3rd Person Animation

end

/---------------------------------------------------------
Name: SWEP:ShootBullet( )
Desc: A convenience function to shoot bullets
---------------------------------------------------------
/
function SWEP:ShootBullet( damage, num_bullets, aimcone )

local bullet = {}
bullet.Num 		= num_bullets
bullet.Src 		= self.Owner:GetShootPos()			// Source
bullet.Dir 		= self.Owner:GetAimVector()			// Dir of bullet
bullet.Spread 	= Vector( aimcone, aimcone, 0 )		// Aim Cone
bullet.Tracer	= 1									// Show a tracer on every x bullets 
bullet.Force	= 10								// Amount of force to give to phys objects
bullet.Damage	= damage
bullet.AmmoType = "Pistol"
bullet.HullSize = 2
bullet.TracerName = "LaserTracer"

self.Owner:FireBullets( bullet )

self:ShootEffects()

end
[/lua]