Use-Activated Timers

Timers aren’t working… I’m gonna try using CurTime() … but that didn’t work either.

What I want to happen is the entity removes after five seconds of a player E-pressing it and holding E constantly. The while loop says that if they release E, then the timer clears and starts over when they re-press.
After 5000 milliseconds (Theoretically) the entity should remove
Although … it doesn’t

Here’s the init.lua, Can anyone tell what’s wrong?

DarkRP mineral deposit entity,
created by: Kreed Izkhanilov

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )


EntModel =

local mineValue
local mineDelete = 5

function ENT:SpawnFunction( ply, tr )
if( !tr.HitWorld ) then return end

	local ent = ents.Create( "mineral_deposit" )

	return ent


function ENT:Initialize()
self:SetModel( table.Random(EntModel) )

	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_NONE )
	self:SetSolid( SOLID_VPHYSICS )
	local phys = self.Entity:GetPhysicsObject()
	self.nodupe = true


function ENT:Use( activator, caller )
if( !activator:IsPlayer() ) then return end
local useStarted = CurTime()

local usingPlayer = activator


function DeleteMine()
Msg( “This mine was removed.” )

function ENT:Think()
local phys = self:GetPhysicsObject()

while !( usingPlayer == nil ) do
local PlayerIsUsing = usingPlayer:KeyDown( IN_USE )
	if !( PlayerIsUsing ) then
		local useStarted = nil
	elseif ( PlayerIsUsing and useStarted < 5000 ) then
		useStarted = useStarted + 1
	if( PlayerIsUsing and useStarted >= 5000 ) then


Try this:

local theActivator
local theEnt
function ENT:Use(activator, caller)
timer.Simple(10, RemoveObject)
theActivator = activator
theEnt = self

function RemoveObject()
if theActivator:KeyPressed("IN_USE") then


Hey! That makes complete sense.

Basically what you’re doing with that is calling a new function which removes the object.

In this new function, if the player is still pressing the use key, then the entity removes.

The only problem I have with it is that I don’t see where you’ve implemented where the timer resets. Other than that, thank you.

With the timer.Simple method, the timer kills itself when it goes off. Yes, that is how the code works, but the timer is still destroyed regardless of whether the user is still pressing the use key after 10 seconds.

So, if I’m understanding you correctly, the code you provided will always base the deletion time on the most recently created timer, correct… in the case that a user presses, releases, and then presses again within 10 seconds?

If they press it twice, it will create 2 different timers that will destroy themselves independently. Still, the entity will not be removed unless the player is still pressing the use key at the time any timer runs out.

A couple problems have arisen…
First off, the entity doesn’t remove at all… and secondly, I’d like to make it so that the timers destroy should the player ever let go of their use key.

Don’t make theEnt local then.

See OP