• Reset Timer on Entity Spawn
    2 replies, posted
I recently threw together an entity that adds money to a user's wallet on ENT:Use. Also on ENT:Use, I have a timer set acting as a cooldown so that it cannot be spammed to just give the user infinite money. I would like a different timer to be set on each entity spawn so that I can have multiple of the same entity each on a different cooldown. Here is my code, any ideas?: AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") util.AddNetworkString( "chatSendReady" ) util.AddNetworkString( "chatSendCooldown" ) local delay = 120 local shouldOccur = true function ENT:Initialize()     self:SetModel("models/props_trainstation/trashcan_indoor001b.mdl")     self:PhysicsInit(SOLID_VPHYSICS)     self:SetMoveType(MOVETYPE_VPHYSICS)     self:SetSolid(SOLID_VPHYSICS)     self:SetUseType( SIMPLE_USE )     local phys = self:GetPhysicsObject()     if phys:IsValid() then         phys:Wake()         phys:EnableMotion(false)     end end function ENT:Use(act , call)     if shouldOccur then         net.Start( "chatSendReady" )         net.Send( Entity( 1 ) )         call:addMoney(1000)         shouldOccur = false         timer.Simple( delay, function() shouldOccur = true end )     else         net.Start( "chatSendCooldown" )         net.Send( Entity( 1 ) )     end end
Why not just set a flag variable to the entity using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetVar]Entity:SetVar[/url]. Then with a simple timer you can reset the flag. So something like self:SetVar("coolDown", true)
Solved this problem. I ended up dropping timers entirely and using a CurTime() comparison thanks to the gentlemen over @ the Garry's Mod Development Discord. Here's how I did it: function ENT:Initialize()     self:SetModel("models/props_junk/garbage128_composite001c.mdl")     self:PhysicsInit(SOLID_VPHYSICS)     self:SetMoveType(MOVETYPE_VPHYSICS)     self:SetSolid(SOLID_VPHYSICS)     self:SetUseType( SIMPLE_USE )     self.BeforeTime = CurTime()     local phys = self:GetPhysicsObject()     if phys:IsValid() then         phys:Wake()     end end local cooldown = 60 function ENT:Use(act , call)     if CurTime() > (self.BeforeTime or 0) + cooldown then         net.Start( "chatSendReadySmall1" )         net.Send( Entity( 1 ) )         self.BeforeTime = CurTime()         call:addMoney(1000)     else         net.Start( "chatSendCooldownSmall1" )         net.Send( Entity( 1 ) )     end end
Sorry, you need to Log In to post a reply to this thread.