• Addind health when Use entity
    5 replies, posted
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: [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 [/CODE]
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?
[CODE]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)[/CODE]
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. [code]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[/code]
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
[url]https://wiki.garrysmod.com/page/Entity/SetUseType[/url] CurTime is the current time of the game.
Sorry, you need to Log In to post a reply to this thread.