Addind health when Use entity

So I am trying to make it when I press E on it it adds health from the value of the variable “healthprogress”
Here is the code:



function ENT:Use(activator, caller)
	if caller:IsPlayer() then
		if caller:Health() < 100 then
			caller:Health() = caller:Health() + healthprogress -- Error says this line.
			caller:ChatPrint('You have collected ' .. healthprogress .. ' health!')
			local healthprogress = 0
		else
			caller:ChatPrint('You already have max health!')
		end
	end
end


healthprogress isn’t defined when you’re using it. Can you show how you’re using the variable outside of this function and if you want it attached to the entity or player?


timer.Create('HealthTimerProgress',.9,0,function() -- Creating a timer so every couple of seconds it adds 1 to the variable healthprogress.
	healthprogress = healthprogress + 1
	end)

So you’re wanting this to be attached to the entity. You’re going to want to store healthprogress in the actual entity userdata instead of a normal variable so it isn’t shared between all instances. You can also use a Think function instead of a timer to make it instanced. Ex.


function ENT:Initialize()
	self.healthprogress = 0
	self.NextAdd = 0
end

function ENT:Use(activator, caller)
	if caller:IsPlayer() then
		if caller:Health() < caller:MaxHealth() then
			caller:SetHealth(caller:Health() + self.healthprogress)
			caller:ChatPrint('You have collected ' .. self.healthprogress .. ' health!')
			self.healthprogress = 0
		else
			caller:ChatPrint('You already have max health!')
		end
	end
end

function ENT:Think()
	if (self.NextAdd <= CurTime()) then
		self.healthprogress = self.healthprogress + 1
		self.NextAdd = CurTime() + 0.9
	end
end

I’m still not sure what curtime means but it works, other than curtime I am starting to understand it, thankyou

[editline]11th February 2017[/editline]

Um isn’t there another use function where it doesn’t like spam it? Cause I would press e on it and it would spam “You have taken 1 health” or whatever. So if I held E on it it would constantly go on and on. How would I fix this

CurTime is the current time of the game.