Make SWEP invisible/remove

Hi, my friend made a SWEP for my model, now we want when it’s empty it become invisible or be removed.
We don’t know how to do that, so does anybody know how?
Here is the script:
[lua]SWEP.Author = “Kriller and Rago”
SWEP.Contact = “yaad182@hotmail.com or kriller_dk@hotmail.com
SWEP.Purpose = “For the laughs”
SWEP.Instructions = “Left click to hit
Right click to throw
Reload to eat”
SWEP.Category = “Crisp sweps”

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = Model(“models/Weapons/v_paprika.mdl”)
SWEP.WorldModel = Model(“models/Crisps/paprika.mdl”)

SWEP.Primary.Clipsize = 1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = “grenade”

SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

function SWEP:Think()
end

function SWEP:Initialize()
util.PrecacheSound(“weapons/iceaxe/iceaxe_swing1.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard1.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard2.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard3.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard4.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard5.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard6.wav”)
util.PrecacheSound(“weapons/knife/knife_slash1.wav”)
util.PrecacheSound(“crisps/eat.wav”)
end

function SWEP:Reload()

if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then return end

self:TakePrimaryAmmo( 1 )
self.Owner:SetHealth(self.Owner:Health()+50)
self.Owner:EmitSound(“crisps/eat.wav”)
end

function SWEP:Deploy()

if self.Owner:GetAmmoCount(self.Primary.Ammo) > 1 then
self:TakePrimaryAmmo((self.Owner:GetAmmoCount(self.Primary.Ammo) - 1))
end

end

function SWEP:PrimaryAttack()

if self.Owner:GetAmmoCount(self.Primary.Ammo) &lt; 1 then return end

self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )

local tr = {}
tr.start = self.Owner:GetShootPos()
tr.endpos = self.Owner:GetShootPos() + ( self.Owner:GetAimVector() * 50 )
tr.filter = self.Owner
tr.mask = MASK_SHOT
local trace = util.TraceLine( tr )

self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)

if ( trace.Hit ) then

	if trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(),"npc") or string.find(trace.Entity:GetClass(),"prop_ragdoll") then
		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  = 1
		bullet.Damage = 20
		self.Owner:FireBullets(bullet)
		self.Weapon:EmitSound( "physics/body/body_medium_impact_hard" .. math.random(1, 6) .. ".wav" )
	else
		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  = 1
		bullet.Damage = 20
		self.Owner:FireBullets(bullet)
		self.Weapon:EmitSound("weapons/knife/knife_slash1.wav")
	end
else
	
end

end

function SWEP:SecondaryAttack()

if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then return end

self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )
self.Weapon:EmitSound(“weapons/iceaxe/iceaxe_swing1.wav”)

local ent = ents.Create("sent_paprika")
local pos = self.Owner:GetShootPos()
ent:SetPos(pos)
ent:SetAngles(self.Owner:EyeAngles())
ent:SetPhysicsAttacker( self.Owner )
ent.owner = self
ent:Spawn()
ent:Activate()
ent:SetOwner(self.Owner)

self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
self.Weapon:SetNextPrimaryFire(CurTime() + 1)

local tr = self.Owner:GetEyeTrace()

local phys = ent:GetPhysicsObject()
if (phys:IsValid()) then
	phys:Wake()
	phys:SetMass(1)
	phys:EnableGravity( true )
	phys:ApplyForceCenter( self:GetForward() *2000 )
	phys:SetBuoyancyRatio( 0 )
end

local shot_length = tr.HitPos:Length()
ent:SetVelocity(self.Owner:GetAimVector():GetNormalized() *  math.pow(shot_length, 3))

self:TakePrimaryAmmo( 1 )

end
[/lua]

Well, impulse 200 makes your weapon invisible, but doesn’t remove it…

What you may want to do is make it switch to a skin with an invisible vMT after the weapon runs out of ammunition.

Why can’t you just add the “holster” tag? That’s the simpler way to do it.

Look here: http://wiki.garrysmod.com/?title=SWEP.Holster

I’m not a real coder, and the real LUA coder is offline but could you edit my friends script and edit it with the holster thingy.

Change line 57 to
if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then self:Remove() return false end

That will remove the gun when it depletes.

EDIT: Do the same for line 102 too…

I prefer using the holdster.

[lua]function SWEP:Think()
if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then
self.Owner:SelectWeapon(self.Owner:GetWeapons( )[math.random(1, #self.Owner:GetWeapons( ))]:GetClass())
end
end[/lua]

put at the bottom of the shared.lua

untested, but it should work.

Report problems to me.

[editline]04:00PM[/editline]

This is ridiculous, the carrier would be holding no weapon as his current weapon was removed. you don’t automatically change weapon when current weapon is removed.

So like this?:
[lua]SWEP.Author = “Kriller and Rago”
SWEP.Contact = “yaad182@hotmail.com or kriller_dk@hotmail.com
SWEP.Purpose = “For the laughs”
SWEP.Instructions = “Left click to hit
Right click to throw
Reload to eat”
SWEP.Category = “Crisp sweps”

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = Model(“models/Weapons/v_paprika.mdl”)
SWEP.WorldModel = Model(“models/Crisps/paprika.mdl”)

SWEP.Primary.Clipsize = 1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = “grenade”

SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

function SWEP:Think()
end

function SWEP:Initialize()
util.PrecacheSound(“weapons/iceaxe/iceaxe_swing1.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard1.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard2.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard3.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard4.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard5.wav”)
util.PrecacheSound(“physics/body/body_medium_impact_hard6.wav”)
util.PrecacheSound(“weapons/knife/knife_slash1.wav”)
util.PrecacheSound(“crisps/eat.wav”)
end

function SWEP:Reload()

if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then return end

self:TakePrimaryAmmo( 1 )
self.Owner:SetHealth(self.Owner:Health()+50)
self.Owner:EmitSound(“crisps/eat.wav”)
end

function SWEP:Deploy()

if self.Owner:GetAmmoCount(self.Primary.Ammo) > 1 then
self:TakePrimaryAmmo((self.Owner:GetAmmoCount(self.Primary.Ammo) - 1))
end

end

function SWEP:PrimaryAttack()

if self.Owner:GetAmmoCount(self.Primary.Ammo) &lt; 1 then return end

self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )

local tr = {}
tr.start = self.Owner:GetShootPos()
tr.endpos = self.Owner:GetShootPos() + ( self.Owner:GetAimVector() * 50 )
tr.filter = self.Owner
tr.mask = MASK_SHOT
local trace = util.TraceLine( tr )

self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)

if ( trace.Hit ) then

	if trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(),"npc") or string.find(trace.Entity:GetClass(),"prop_ragdoll") then
		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  = 1
		bullet.Damage = 20
		self.Owner:FireBullets(bullet)
		self.Weapon:EmitSound( "physics/body/body_medium_impact_hard" .. math.random(1, 6) .. ".wav" )
	else
		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  = 1
		bullet.Damage = 20
		self.Owner:FireBullets(bullet)
		self.Weapon:EmitSound("weapons/knife/knife_slash1.wav")
	end
else
	
end

end

function SWEP:SecondaryAttack()

if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then return end

self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )
self.Weapon:EmitSound(“weapons/iceaxe/iceaxe_swing1.wav”)

local ent = ents.Create("sent_paprika")
local pos = self.Owner:GetShootPos()
ent:SetPos(pos)
ent:SetAngles(self.Owner:EyeAngles())
ent:SetPhysicsAttacker( self.Owner )
ent.owner = self
ent:Spawn()
ent:Activate()
ent:SetOwner(self.Owner)

self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
self.Weapon:SetNextPrimaryFire(CurTime() + 1)

local tr = self.Owner:GetEyeTrace()

local phys = ent:GetPhysicsObject()
if (phys:IsValid()) then
	phys:Wake()
	phys:SetMass(1)
	phys:EnableGravity( true )
	phys:ApplyForceCenter( self:GetForward() *2000 )
	phys:SetBuoyancyRatio( 0 )
end

local shot_length = tr.HitPos:Length()
ent:SetVelocity(self.Owner:GetAimVector():GetNormalized() *  math.pow(shot_length, 3))

self:TakePrimaryAmmo( 1 )

end

function SWEP:Think()
if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then
self.Owner:SelectWeapon(self.Owner:GetWeapons( )[math.random(1, #self.Owner:GetWeapons( ))]:GetClass())
end
end
[/lua]

yeah like that, try it

[editline]04:21PM[/editline]

[lua]function SWEP:Think()
if SERVER then
if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then
self.Owner:SelectWeapon(self.Owner:GetWeapons( )[math.random(1, #self.Owner:GetWeapons( ))]:GetClass())
end
end
end[/lua]
code fixed for client

Grazie

Script doesn’t work can somebody help?

I know, he wanted the weapon to disappear.
He never mentioned other weapon should take place.
He said the weapon could be invisible or removed … mine does both.
But I admin, there are still flaws. Mine would work only when player has shot once with an empty gun(fixable). And I forgot to add client check(also fixable).

And to be honest your random weapon selection is … ridiculous too :slight_smile:

Almost as ridiculous as say, selecting a random weapon? :monocle:

Or not be a dumb ass and do:

[lua]self.Owner:DropWeapon( self )
self:Remove()[/lua]

to select the next-best weapon.

Getting thread back alive, still haven’t found solution, I want something that is in this script:
[lua]

if ( !addon.IsFeatureEnabled( “Garry’s Mod Plus”, “HL2: Episode 2 Content” ) ) then

return

end

if ( !util.IsValidModel( “models/weapons/v_magnade.mdl” ) ) then

return

end

if ( SERVER ) then

AddCSLuaFile( "shared.lua" )

end

if ( CLIENT ) then

SWEP.PrintName			= "MAGNADE"
SWEP.Author				= "VALVe"

end

SWEP.Base = “swep_frag”
SWEP.Category = “Half-Life 2”

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_magnade.mdl”
SWEP.WorldModel = “models/weapons/w_magnade.mdl”

SWEP.Primary.AmmoType = “weapon_striderbuster”

function SWEP:Operator_HandleAnimEvent( pEvent, pOperator )

if ( self.fThrewGrenade ) then
	return;
end

local pOwner = self.Owner;
self.fThrewGrenade = false;

if( pEvent ) then
	if pEvent == "EVENT_WEAPON_SEQUENCE_FINISHED" then
		self.m_fDrawbackFinished = true;
		return;

	elseif pEvent == "EVENT_WEAPON_THROW" then
		self:ThrowGrenade( pOwner );
		self:DecrementAmmo( pOwner );
		self.fThrewGrenade = true;
		return;

	else
		return;
	end
end

local RETHROW_DELAY = self.Primary.Delay
if( self.fThrewGrenade ) then
self.Weapon:SetNextPrimaryFire( CurTime() + RETHROW_DELAY );
self.m_flNextPrimaryAttack = CurTime() + RETHROW_DELAY;
self.m_flTimeWeaponIdle = FLT_MAX;
end

end

function SWEP:SecondaryAttack()
end

function SWEP:Think()

if ((self.fThrewGrenade && CurTime() &gt; self.Primary.Delay)) then
	self.fThrewGrenade = false;

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay );
	self.m_flNextPrimaryAttack = CurTime() + self.Primary.Delay;
	self.m_flTimeWeaponIdle = CurTime() + self.Primary.Delay;
end

if ((self.m_flSequenceDuration &gt; CurTime())) then
	self:Operator_HandleAnimEvent( "EVENT_WEAPON_SEQUENCE_FINISHED" );
	self.m_flSequenceDuration = CurTime();
end

if( self.m_fDrawbackFinished ) then
	local pOwner = self.Owner;

	if (pOwner) then
		if( self.m_AttackPaused ) then
		if self.m_AttackPaused == GRENADE_PAUSED_PRIMARY then
			if( !(pOwner:KeyDown( IN_ATTACK )) ) then
				self.Weapon:SendWeaponAnim( ACT_VM_THROW );
				self:Operator_HandleAnimEvent( "EVENT_WEAPON_THROW" );

				self.m_fDrawbackFinished = false;
			end
			return;

		else
			return;
		end
		end
	end
end

local pPlayer = self.Owner;

if ( !pPlayer ) then
	return;
end

if ( self.m_bRedraw ) then
	self:Reload();
end

end
[/lua]

Uh, isn’t any of the above solutions enough and if not then why?

Well first, yours isn’t really exactly what we want.
Second, Falco’s doesn’t work.

What about SteveUK’s?

Oh yeah, where should I put those 2 lines?

Right, so i am the coder, i tried using all the ideas listed above, none worked. Any other ideas?