• how to use math.approach?
    6 replies, posted
i was trying to add a script that automatically regenerates health, ive seen people use math.approach but i cant really get it, this doesnt work and i dont know when to call it [CODE] function regenhealth(ply) local health = ply:Health() local maxhealth = 100 local speed = 200 if health < 100 then health = math.Approach( health, maxhealth, speed * FrameTime() ) end end [/CODE]
You'll need to call ply:SetHealth( health ) afterwards.
thanks [editline]7th March 2015[/editline] but now i need to have it go automatically, does anyone know how? or what hook to use
You need to use it in a Think hook, here is an example; [lua]local speed = 67 local health local GetMaxHealth, Health, SetHealth, player, FrameTime = GetMaxHealth, Health, SetHealth, player, FrameTime hook.Add("Think", "kden", function() for k,v in pairs(player.GetAll()) do health = v:Health() if (health < v:GetMaxHealth()) then health = math.Approach(health, v:GetMaxHealth(), (speed * FrameTime())) v:SetHealth(health) end end end)[/lua] It regenerates fast depending on your tickrate. Right now the minimum speed is 67, because the tickrate is 66 (for me that is), however, if your server has tickrate 33, you could try to set the speed to 34. Still, it would regenerate very fast, so I'd suggest using timers to regenerate instead depending on how fast you want it to actually regenerate.
[QUOTE=Author.;47277184]You need to use it in a Think hook, here is an example; [lua]local speed = 67 local health local GetMaxHealth, Health, SetHealth, player, FrameTime = GetMaxHealth, Health, SetHealth, player, FrameTime hook.Add("Think", "kden", function() for k,v in pairs(player.GetAll()) do health = v:Health() if (health < v:GetMaxHealth()) then health = math.Approach(health, v:GetMaxHealth(), (speed * FrameTime())) v:SetHealth(health) end end end)[/lua] It regenerates fast depending on your tickrate. Right now the minimum speed is 67, because the tickrate is 66 (for me that is), however, if your server has tickrate 33, you could try to set the speed to 34. Still, it would regenerate very fast, so I'd suggest using timers to regenerate instead depending on how fast you want it to actually regenerate.[/QUOTE] ok how would i find the tickrate
I'd suggest you use the PlayerTick hook actually. You can use engine.TickInterval to get the delay between ticks but really all you need is the PlayerTick hook and the FrameTime function. [code] hook.Add( "PlayerTick", "My foooadaa", function( ply ) ply:SetHealth( math.Approach( ply:GetHealth(), 100, 10 * FrameTime() ) ) end ) [/code] This example increases the player's health by 10 per second i hope
Thanks
Sorry, you need to Log In to post a reply to this thread.