timer.Simple problem

I’m trying to make a prop throwing Garry’s Mod SWEP but I have a problem. Someone can just spam the gun and crash the game. So I did some research and found the timer.Simple library. So I tried and and it didn’t work…

Here is my code:


local ShootSound = "weapons/one piece/fist/pistal.mp3"
local DeploySound = "weapons/one piece/fist/Gomu Gomu no.mp3"

function SWEP:Deploy()
		self:EmitSound( DeploySound )
end

function SWEP:PrimaryAttack()

	-- This weapon is 'automatic'. This function call below defines
	-- the rate of fire. Here we set it to shoot every 0.1 seconds.
	self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )

	-- Call 'ThrowChair' on self with this model
	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )
	
	timer.Simple(4, function()
	if self.ThrowChair:IsValid() then self.ThrowChair:Remove() end
	end)

end

--
-- Called when the rightmouse button is pressed
--
function SWEP:SecondaryAttack()

	-- Note we don't call SetNextSecondaryFire here because it's not
	-- automatic and so we let them fire as fast as they can click.

	-- Call 'ThrowChair' on self with this model
	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )

	timer.Simple(4, function()
	if self.ThrowChair:IsValid() then self.ThrowChair:Remove() end
	end)

end

--
-- A custom function we added. When you call this the player will fire a chair!
--
function SWEP:ThrowChair( model_file )

	--
	-- Play the shoot sound we precached earlier!
	--
	self:EmitSound( ShootSound )


	--
	-- If we're the client then this is as much as we want to do.
	-- We play the sound above on the client due to prediction.
	-- ( if we didn't they would feel a ping delay during multiplayer )
	--
	if ( CLIENT ) then return end

	--
	-- Create a prop_physics entity
	--
	local ent = ents.Create( "prop_physics" )

	--
	-- Always make sure that created entities are actually created!
	--
	if ( !IsValid( ent ) ) then return end

	--
	-- Set the entity's model to the passed in model
	--
	ent:SetModel( model_file )

	--
	-- Set the position to the player's eye position plus 16 units forward.
	-- Set the angles to the player'e eye angles. Then spawn it.
	--
	ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
	ent:SetAngles( self.Owner:EyeAngles() )
	ent:Spawn()


	--
	-- Now get the physics object. Whenever we get a physics object
	-- we need to test to make sure its valid before using it.
	-- If it isn't then we'll remove the entity.
	--
	local phys = ent:GetPhysicsObject()
	if ( !IsValid( phys ) ) then ent:Remove() return end


	--
	-- Now we apply the force - so the chair actually throws instead
	-- of just falling to the ground. You can play with this value here
	-- to adjust how fast we throw it.
	--
	local velocity = self.Owner:GetAimVector()
	velocity = velocity * 10000
	velocity = velocity + ( VectorRand() * 100 ) -- a random element
	phys:ApplyForceCenter( velocity )

	--
	-- Assuming we're playing in Sandbox mode we want to add this
	-- entity to the cleanup and undo lists. This is done like so.
	--
	cleanup.Add( self.Owner, "props", ent )

	undo.Create( "Thrown_punch" )
		undo.AddEntity( ent )
		undo.SetPlayer( self.Owner )
	undo.Finish()
end

Here is were the timer.Simple code is:


function SWEP:PrimaryAttack()

	-- This weapon is 'automatic'. This function call below defines
	-- the rate of fire. Here we set it to shoot every 0.1 seconds.
	self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )

	-- Call 'ThrowChair' on self with this model
	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )
	
	timer.Simple(4, function()
	if self.ThrowChair:IsValid() then self.ThrowChair:Remove() end
	end)

end

--
-- Called when the rightmouse button is pressed
--
function SWEP:SecondaryAttack()

	-- Note we don't call SetNextSecondaryFire here because it's not
	-- automatic and so we let them fire as fast as they can click.

	-- Call 'ThrowChair' on self with this model
	self:ThrowChair( "models/props/luffy/fist/fist.mdl" )

	timer.Simple(4, function()
	if self.ThrowChair:IsValid() then self.ThrowChair:Remove() end
	end)

end

self.ThrowChair is the function that throws the chair, not the chair entity itself. If I were doing this I’d create the timer in the ThrowChair function where the ent is available.

I didn’t work.


function SWEP:ThrowChair( model_file )

	--
	-- Play the shoot sound we precached earlier!
	--
	self:EmitSound( ShootSound )


	--
	-- If we're the client then this is as much as we want to do.
	-- We play the sound above on the client due to prediction.
	-- ( if we didn't they would feel a ping delay during multiplayer )
	--
	if ( CLIENT ) then return end

	--
	-- Create a prop_physics entity
	--
	local ent = ents.Create( "prop_physics" )

	--
	-- Always make sure that created entities are actually created!
	--
	if ( !IsValid( ent ) ) then return end

	--
	-- Set the entity's model to the passed in model
	--
	ent:SetModel( model_file )

	--
	-- Set the position to the player's eye position plus 16 units forward.
	-- Set the angles to the player'e eye angles. Then spawn it.
	--
	ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
	ent:SetAngles( self.Owner:EyeAngles() )
	ent:Spawn()


	--
	-- Now get the physics object. Whenever we get a physics object
	-- we need to test to make sure its valid before using it.
	-- If it isn't then we'll remove the entity.
	--
	local phys = ent:GetPhysicsObject()
	if ( !IsValid( phys ) ) then ent:Remove() return end


	--
	-- Now we apply the force - so the chair actually throws instead
	-- of just falling to the ground. You can play with this value here
	-- to adjust how fast we throw it.
	--
	local velocity = self.Owner:GetAimVector()
	velocity = velocity * 1000000000
	velocity = velocity + ( VectorRand() * 1000 ) -- a random element
	phys:ApplyForceCenter( velocity )
	
	
	timer.Simple(2.5, function()
	if self.ThrowChair:IsValid() then self.ThrowChair:Remove() end
	end)
	--
	-- Assuming we're playing in Sandbox mode we want to add this
	-- entity to the cleanup and undo lists. This is done like so.
	--
	cleanup.Add( self.Owner, "props", ent )

	undo.Create( "Thrown_punch" )
		undo.AddEntity( ent )
		undo.SetPlayer( self.Owner )
	undo.Finish()
end

[lua]
timer.Simple(2.5, function()
if ent:IsValid() then ent:Remove() end
end)
[/lua]

You still had it referencing the function, not the actual entity

Thanks Beast! It worked!