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?

[lua]
–[[
DarkRP mineral deposit entity,
created by: Kreed Izkhanilov
]]–

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

include(‘shared.lua’)

EntModel =
{
“models/Cliffs/rocks_medium01.mdl”,
“models/Cliffs/rocks_large01.mdl”,
“models/Cliffs/rocks_large02.mdl”,
“models/Cliffs/rocks_large03.mdl”,
“models/props_mining/caverocks_cluster01.mdl”,
“models/props_mining/caverocks_cluster02.mdl”,
“models/props_mining/rock_caves01.mdl”,
“models/props_mining/rock_caves01a.mdl”,
“models/props_canal/rock_riverbed02b.mdl”,
“models/props_wasteland/rockgranite02c.mdl”,
“models/props_wasteland/rockgranite03c.mdl”,
“models/props_foliage/rock_forest02.mdl”,
“models/props_foliage/rock_forest01a.mdl”,
“models/props_foliage/rock_forest01.mdl”
}

local mineValue
local mineDelete = 5

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

	local ent = ents.Create( "mineral_deposit" )
	ent:SetPos(tr.HitPos)
	ent:Spawn()

	return ent

end

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

end

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

local usingPlayer = activator

end

function DeleteMine()
self:Remove()
Msg( “This mine was removed.” )
end

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
	end
	
	if( PlayerIsUsing and useStarted >= 5000 ) then
		DeleteMine()
	end
end

end
[/lua]

Try this:



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

function RemoveObject()
if theActivator:KeyPressed("IN_USE") then
theEnt:Remove()
end
end

end


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