Timer Help!

Working on a new ent for DarkRP.
its supposed to spawn a “normDrug” after every thirty seconds. but it only does it once, i know the problum but i dont know how to fix it.
How do i Reset a Timer so it goes through the timer again and again and again?
heres what i got:



function ENT:createDrug()
	self.Entity.CanUse = true
	local userb = self.drug_user
	local drugPos = self:GetPos()
	timer.Create( "DRtimer", 30, 0, function()
	drug = ents.Create("normdrug")
	drug:SetPos(Vector(drugPos.x,drugPos.y,drugPos.z + 35))
	drug.dt.owning_ent = userb
	drug.SID = userb.SID
	drug.ShareGravgun = true
	drug.nodupe = true
	drug:Spawn()
	self.sparking = false
		end
	end
end


thanks for helping

Currently you’ve set it so it runs infinitely. I don’t see what the issue is.

However, you could be getting issues where your entity can move but drugPos does not update. Rather than declare the variable on ENT:createDrug(), declare it inside the timer function so it gets updated when the timer runs.

my problem is, if it is set to run infinitely, wont it run once every time the server calls a think? that would cause major lag after a bit.



function ENT:createDrug()
	timer.Create( "DRtimer", 30, 0, function()
        self.Entity.CanUse = true
	local userb = self.drug_user
	local drugPos = self:GetPos()
	drug = ents.Create("normdrug")
	drug:SetPos(Vector(drugPos.x,drugPos.y,drugPos.z + 35))
	drug.dt.owning_ent = userb
	drug.SID = userb.SID
	drug.ShareGravgun = true
	drug.nodupe = true
	drug:Spawn()
	self.sparking = false
		end
	end
end


so your telling me that this code will run again after it stops every time, since the number of repeats is set to 0…

You never closed the timer.

[editline]01:16PM[/editline]



function ENT:createDrug()
	timer.Create>>(<< open "DRtimer", 30, 0, function()
        self.Entity.CanUse = true
	local userb = self.drug_user
	local drugPos = self:GetPos()
	drug = ents.Create("normdrug")
	drug:SetPos(Vector(drugPos.x,drugPos.y,drugPos.z + 35))
	drug.dt.owning_ent = userb
	drug.SID = userb.SID
	drug.ShareGravgun = true
	drug.nodupe = true
	drug:Spawn()
	self.sparking = false
	end >>)<< closed
end


[lua]
function ENT:createDrug()
timer.Create( “DRtimer”, 30, 0, function()
self.Entity.CanUse = true
local userb = self.drug_user
local drugPos = self:GetPos()
drug = ents.Create(“normdrug”)
drug:SetPos(Vector(drugPos.x,drugPos.y,drugPos.z + 35))
drug.dt.owning_ent = userb
drug.SID = userb.SID
drug.ShareGravgun = true
drug.nodupe = true
drug:Spawn()
self.sparking = false
end )
end
[/lua]

Thanks. that would have caused trouble

Im guessing you’re using Notepad++, as it doesent quite now the difference between ) and end, so it stopped the timer at your 2nd “end” which should have been an “)”.