create.Timer problem

So i was making food for the cook job in DarkRP, and i wanted to make milk that would regen 1hp for 100 seconds. For some reaosn when i press e on it, it waits the one second and then failes to continue, any help?

The Function:


function ENT:Use(activator)
local IDSteam = string.gsub(activator:SteamID(), ":", "")
local health = activator:Health()

	if activator:Health() < 100 then
		timer.Create( IDSteam .. "HpRegen" , 1, 100, function() activator:SetHealth(health+1) end)
	end
	
	self.Entity:Remove()
end

-fuck-

Unfortunately, i have it right, you don’t think i would have checked for that? Number of reps right before function and before that is interval of the function, so this is still unsolved

Edit: I also tried removing the steamid thingy, nothing changed from it.

[lua]function ENT:Use(activator)
local IDSteam = string.gsub(activator:SteamID(), “:”, “”)

timer.Create( IDSteam … “HpRegen” , 1, 0, function()
local health = activator:Health()
if health < 100 then
activator:SetHealth(health+1)
else
timer.Destroy( IDSteam … “HPRegen” )
self.Entity:Remove()
end

end)

end[/lua]
That should work
Sorry I made so many mistakes having a bad time thinking. Need some sleep :frowning:

Oh no, don’t worry about it, i was thinking about doing this but its just much more clean to make one timer, alright ill try this out thanks.

You’re only setting the players health up by 1 for 100 seconds. You’re not actually increasing it.

[lua]
local ply = activator
timer.Create(ply:EntIndex()…“hp_regen”, 1, 100, function()

--Destroy the timer if the player reaches their max hp
if ply:Health() &gt;= (ply:GetMaxHealth() or 100) then
	timer.Destroy(ply:EntIndex().."hp_regen")
	return
end

ply:SetHealth(ply:Health()+1)

end)
[/lua]

Alright, so i modded it to a fashion that i liked it to, and its still having the same problem


function ENT:Use(activator)
local IDSteam = string.gsub(activator:SteamID(), ":", "")
local health = activator:Health()

if activator:Health() >= 100 then timer.Destroy( IDSteam .. "HPRegen" ) return else
timer.Create( IDSteam .. "HpRegen" , 1, 0, function() activator:SetHealth(health+1) end )
	self.Entity:Remove()
end
end


[editline]14th March 2014[/editline]

@Brandon, but health is updated every time there is a +1

No it’s not, you’re not understanding the logic.

You localized health, you made a timer, then by doing “ply:SetHealth(hp+1)”, you’re just doing “ply:SetHealth(44+1)” for example.

Alright, ill test this out, thanks for the help

So this :


function ENT:Use(activator)
local ply = activator
timer.Create(ply:EntIndex().."hp_regen", 1, 100, function()

	--Destroy the timer if the player reaches their max hp
	if ply:Health() >= 100 then
		timer.Destroy(ply:EntIndex().."hp_regen")
		return
	end
	self.Entity:Remove()
	ply:SetHealth(ply:Health()+1)
end)
end

still does not work, once the entity removes the hp regen stops

But if he is setting the players health with the old value surely it doesn’t matter?

It’s not like he is doing health = health + 1

Edit: I understand what you meant now, sorry pretty tired, got the wrong idea about your post.

Okay, you don’t understand either.

If you localize it outside the timer, it’s being localized once, so it will equal that value ALL THE TIME if you use it inside the timer.

As stated before, he was literally just doing 44+1 for 100 seconds.

Yes I understand, just the way you phrased it made me think you meant something else xD

That’s not even the code I gave you.

This may be more favorable. WoW-style healing-over-time.
[lua]
function ENT:Use(activator)
local pl = activator;
local timerName = (pl:UniqueID() … “.HPRegen.” … CurTime());

timer.Create(timerName, 1, 100, function()
	if (!pl:IsValid()) then
		timer.Destroy(timerName);
		return;
	end
	
	local newHealth = math.Clamp(pl:Health() + 1, 0, 100); -- Emulates a WoW-like healing. It'll keep healing you for 100 seconds, just not above 100%.
	
	pl:SetHealth(newHealth);
end);

end
[/lua]

Brandons timer was better. Its useless calling the timer 100 times in excess and may even cause lag if overused, checking for HP and destroying the timer is the best option.

Not to beat a dead horse since the thread’s inactive now, but you aren’t going to cause any lag with that.

And the point of the timer I gave was that it would keep calling for 100s even if you’re full health. Like I said it’s healing-over-time. So if you’re full at some point during the time frame it just won’t affect you but if you take damage during the time frame it’ll heal it. Like I said (again), WoW style.

Some MMOs have a heal-over-time which de-activate if you’re at max health, others will stay active until the timer is up. It’s really just what taste of effect you want.