Need help creating something to instantly regenerate health every 3-4 seconds
23 replies, posted
As the title suggests, I need assistance in creating something that instantly regenerates your health in 3 or 4 seconds. I tried using timers but I couldn't get them to work, I am rather new to Lua and don't know what could be wrong as no errors are spit into console either. Any help would be greatly appreciated!
What do you mean by "something?" Like an entity?
Regenerate.. Just use the EntityTakeDamageHook and create a timer.simple wo runs the ply:MaxHealth stuff....
[QUOTE=code_gs;52613436]What do you mean by "something?" Like an entity?[/QUOTE]
I am referring to the player; to go into depth, let's say he gets damaged. In a couple seconds he instantly regenerates to a preset value. The something I am referring to is a SWEP, that when held, does this, to elaborate. The SWEP I am working on works perfectly fine but I still need to implement this feature.
Should this happen when the SWEP is equipped or just on the player? If the former, should it be automatic or require them to hold on a fire key?
[QUOTE=code_gs;52613567]Should this happen when the SWEP is equipped or just on the player? If the former, should it be automatic or require them to hold on a fire key?[/QUOTE]
it should happen passively while the swep is equipped
[code]local flRate = 1
local iAmount = 1
-- Serverside
hook.Add("Think", "HealthRegen", function()
local tPlayers = player.GetAll()
local flCurTime = CurTime()
for i = 1, #tPlayers do
local pPlayer = tPlayers[i]
-- Heal if they have the weapon passively
if (pPlayer:HasWeapon("weapon_whatever")) then
local flNextRegen = pPlayer.m_flNextRegen
-- Regen time hasn't been set yet or it's time for another heal
if (flNextRegen == nil or flNextRegen <= flCurTime) then
local iHealth = pPlayer:Health()
local iNewHealth = math.min(iHealth + iAmount, pPlayer:GetMaxHealth())
-- If the player isn't at max health
if (iHealth ~= iNewHealth) then
pPlayer:SetHealth(iNewHealth)
end
-- Heal again after flRate seconds
pPlayer.m_flNextRegen = flCurTime + flRate
end
end
end
end)[/code]
[QUOTE=code_gs;52613891][code]local flRate = 1
local iAmount = 1
-- Serverside
hook.Add("Think", "HealthRegen", function()
local tPlayers = player.GetAll()
local flCurTime = CurTime()
for i = 1, #tPlayers do
local pPlayer = tPlayers[i]
-- Heal if they have the weapon passively
if (pPlayer:HasWeapon("weapon_whatever")) then
local flNextRegen = pPlayer.m_flNextRegen
-- Regen time hasn't been set yet or it's time for another heal
if (flNextRegen == nil or flNextRegen <= flCurTime) then
local iHealth = pPlayer:Health()
local iNewHealth = math.min(iHealth + iAmount, pPlayer:GetMaxHealth())
-- If the player isn't at max health
if (iHealth ~= iNewHealth) then
pPlayer:SetHealth(iNewHealth)
end
-- Heal again after flRate seconds
pPlayer.m_flNextRegen = flCurTime + flRate
end
end
end
end)[/code][/QUOTE]
Oh! Thank you so much. I'll test that out right now!
Edit: works excellently! Thanks again! I'll try and make it regenerate armour as well now.
[editline]25th August 2017[/editline]
[QUOTE=code_gs;52613891][code]local flRate = 1
local iAmount = 1
-- Serverside
hook.Add("Think", "HealthRegen", function()
local tPlayers = player.GetAll()
local flCurTime = CurTime()
for i = 1, #tPlayers do
local pPlayer = tPlayers[i]
-- Heal if they have the weapon passively
if (pPlayer:HasWeapon("weapon_whatever")) then
local flNextRegen = pPlayer.m_flNextRegen
-- Regen time hasn't been set yet or it's time for another heal
if (flNextRegen == nil or flNextRegen <= flCurTime) then
local iHealth = pPlayer:Health()
local iNewHealth = math.min(iHealth + iAmount, pPlayer:GetMaxHealth())
-- If the player isn't at max health
if (iHealth ~= iNewHealth) then
pPlayer:SetHealth(iNewHealth)
end
-- Heal again after flRate seconds
pPlayer.m_flNextRegen = flCurTime + flRate
end
end
end
end)[/code][/QUOTE]
I've gotten it to regenerate armor, but is there a way to make it only regenerate while the SWEP is active? It's healing me while I have another weapon out.
[editline]25th August 2017[/editline]
interesting... i have two different SWEPS which use the script, and one stops functioning and one starts working after I make an edit to the code...
Use GetActiveWeapon than HasWeapon
[QUOTE=gonzalolog;52614105]Use GetActiveWeapon than HasWeapon[/QUOTE]
i've tried doing [CODE] if (pPlayer:GetActiveWeapon() == "weapon_gladius") then [/CODE]
but that didn't work for me, I wouldn't heal at all. I'm still very new to this so I don't know what I did wrong here
GetActiveWeapon returns the entity, not the class you are comparing, add :GetClass() to that GetActiveWeapon to make the entity return it class
[QUOTE=gonzalolog;52614189]GetActiveWeapon returns the entity, not the class you are comparing, add :GetClass() to that GetActiveWeapon to make the entity return it class[/QUOTE]
i'll try that, thanks
[editline]25th August 2017[/editline]
I've tried [CODE]if (pPlayer:GetActiveWeapon():GetActiveClass():GetClass() == "weapon_gladius") then[/CODE] to no avail either
[QUOTE=Asian Aimbot;52614356]i'll try that, thanks
[editline]25th August 2017[/editline]
I've tried to no avail either[/QUOTE]
[CODE]if (pPlayer:GetActiveWeapon():GetClass() == "weapon_gladius") then[/CODE]
There is no such thing as GetActiveClass. You're looking to direct apply [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetClass]Entity:GetClass[/url] to the entity itself (in this case the player's active weapon -- this is also exampled in the description on the wiki)
[QUOTE=kpjVideo;52614375][CODE]if (pPlayer:GetActiveWeapon():GetClass() == "weapon_gladius") then[/CODE]
There is no such thing as GetActiveClass[/QUOTE]
whoopsies i confused GetClass and GetActiveWeapon I kinda squished them together
[editline]25th August 2017[/editline]
[CODE]if (pPlayer:GetActiveWeapon():GetClass() == "weapon_praetorian_gladius") then[/CODE] also doesn't work for me either
[editline]25th August 2017[/editline]
Do I have to have the healing script under some kind of function like SWEP.Think()?
That will only work if the weapon is out.
[editline]26th August 2017[/editline]
In which case, you should use SWEP:Think. You described originally, though, that you wanted it to work passively.
[QUOTE=code_gs;52614480]That will only work if the weapon is out.
[editline]26th August 2017[/editline]
In which case, you should use SWEP:Think. You described originally, though, that you wanted it to work passively.[/QUOTE]
That was poor wording on my part, I meant it should work passively while the weapon is out. I've tried SWEP:Think to constantly set armor/health to 100, however due to the nature of it, it instantly did so every tick and thus the wearer became practically invincible.
[QUOTE=Asian Aimbot;52614505]That was poor wording on my part, I meant it should work passively while the weapon is out. I've tried SWEP:Think to constantly set armor/health to 100, however due to the nature of it, it instantly did so every tick and thus the wearer became practically invincible.[/QUOTE]
You can use a cooldown system. Where you have a variable on the SWEP which you define in the SWEP:Think() if its nil and the when its less than CurTime() the code runs again
[code]
function SWEP:Think()
if !self.cooldown then self.cooldown = CurTime() -1 end
if self.cooldown < CurTime() then
-- Run Code
self.cooldown = CurTime() + 10 -- How many seconds you want it not to run the code
end
end
[/code]
urg why can't I use tabs
[QUOTE=no1star;52615048]urg why can't I use tabs[/QUOTE]
Because tabs are shit.
[QUOTE=no1star;52615048]You can use a cooldown system. Where you have a variable on the SWEP which you define in the SWEP:Think() if its nil and the when its less than CurTime() the code runs again
[code]
function SWEP:Think()
if !self.cooldown then self.cooldown = CurTime() -1 end
if self.cooldown < CurTime() then
-- Run Code
self.cooldown = CurTime() + 10 -- How many seconds you want it not to run the code
end
end
[/code]
urg why can't I use tabs[/QUOTE]
You should put the cooldown on the owner, not the weapon.
[editline]26th August 2017[/editline]
Also, there's no need to do that if cooldown doesn't exist: just pair the if-statements.
[QUOTE=code_gs;52615054]You should put the cooldown on the owner, not the weapon.
[editline]26th August 2017[/editline]
Also, there's no need to do that if cooldown doesn't exist: just pair the if-statements.[/QUOTE]
why not on the weapon? I don't see why it needs to be on the player when we are running it of a variable which will only exist on the weapon. And so I don't want the variable to carry on existing when the weapon is gone. Or is there something that I don't know
and the if statements is style of code just out of habit
[QUOTE=no1star;52615382]why not on the weapon? I don't see why it needs to be on the player when we are running it of a variable which will only exist on the weapon. And so I don't want the variable to carry on existing when the weapon is gone. Or is there something that I don't know
and the if statements is style of code just out of habit[/QUOTE]
Because then if another player picks up the weapon, the cooldown from the previous player would be applied since it's carried on the weapon.
I'll try some of these, I'll report back to you on the results once I figure it out
[CODE]function SWEP:Think()
if !self.cooldown then self.cooldown = CurTime() -1 end
if self.cooldown < CurTime() then
self.Owner:SetHealth("200")
self.cooldown = CurTime() + 4 -- How many seconds you want it not to run the code
end
end[/CODE]
works perfectly, but how do I set it to the player?
[QUOTE=code_gs;52615830]Because then if another player picks up the weapon, the cooldown from the previous player would be applied since it's carried on the weapon.[/QUOTE]
... I just asked quite a few developers and every single one thought that it creates a new entity. Unless you can prove to me that is the case, I will be very surprised if it is.
My main reason behind this is this were true then why would gmod waste the process of "remaking" data tables when a player picks up the weapon? And then you could say the same argument if you have it on the player. that it would be carried over. But from what I have seen with also ammo not carrying over.
And I will alter my answer if I am indeed incorrect.
I honestly have no idea what you're saying. All I suggested is that storing the cooldown on the player (which can be accessed with self:GetOwner() in a weapon hook) would make the cooldown player-isolated so that it is properly maintained in the case the weapon is dropped and another player picks it up. No new entities or datatables are being created. It's as simple as
[code]function SWEP:Think()
local pPlayer = self:GetOwner()
if (pPlayer:IsValid()) then
local flCooldown = pPlayer.m_flHealCooldown
local flCurTime = CurTime()
if (flCooldown == nil or flCooldown <= flCurTime) then
-- Do stuff
pPlayer.m_flHealCooldown = flCurTime + 1
end
end
end[/code]
Sorry, you need to Log In to post a reply to this thread.