• Need help to improve a script related to the Health
    18 replies, posted
Hey, It's again me. hook.Add("Think", "PlayTimeCheck", function() for k, v in ipairs(player.GetAll()) do if (v:Health() <= 10) then if (!timer.Exists("10HPKill-" .. v:SteamID())) then timer.Create("10HPKill-" .. v:SteamID(), 120, 1, function() if (IsValid(v)) then v:Kill() end end) end else timer.Remove("10HPKill-" .. v:SteamID()) end end end) I decided to modify a little bit the scrip to make them die more fast if they are low. I don't really know where could I place that : local delay = 0 hook.Add( "Think", "CurTimeDelay", function() if CurTime() < delay then return end v:SetHealth(v:Health-1) delay = CurTime() + 12 end ) CurTime I want to make him will lose 1HP every 12 seconds when timer.Exists. In example : If he is at 5hp, he will die in 60s...
Use this hook GM:EntityTakeDamage and check if the entity is a player and then check his/her health and add a timer that repeats every 12 seconds. Remove the timer if the player is dead or invalid. hook.Add("EntityTakeDamage", "my shitty dmg hook", function(entity, dmginfo) if (IsValid(entity) and entity:IsPlayer() and entity:Health() <= 10 and !timer.Exists("fkintimer")) timer.Create("fkintimer", 12, 0, function() if (!IsValid(entity)) then timer.Remove("fkintimer"); return; end if (entity:Alive()) then timer.Remove("fkintimer"); return; end entity:Kill(); timer.Remove("fkintimer") return; end end); end end);
Thanks for your replied, i tried your script but he doesn't work like I want. When you have HP<10, you died in 12 seconds. It's not really my wish. The first script, I just want to decrease his life when he is HP<10
oh my bad, change the timer interval to 1.2 seconds
It kills me instant :/
no, i edited my post lol
Still doesn't work, when i have HP<10, I lose 1 HP every second and die. There is no different between 7HP or 4HP
use your math skills and calculate how many seconds it takes to die in 12 seconds based on the players current health and then use that number in the timer interval
No no, I don't want to kill them in 12 seconds. When they have Hp<=10. They lose 1 HP every 12 seconds.
change the timer interval to 12 seconds then?
timer.Create(timerID, 12, 12, function() It's working but It's continu to take my hp When I respawn
You don't want the timer to repeat 12 times so change the second 12 to 0 - this makes it repeat indefinitely and we handle cancelling it inside the timer. Add a call to timer.Remove if the player's health is above 10, that way if they respawn or heal above 10 health, the timer will stop running. Updated code: hook.Add("EntityTakeDamage", "my shitty dmg hook", function(entity, dmginfo) if (IsValid(entity) and entity:IsPlayer() and entity:Health() <= 10) then local timerID = "fktimer" .. entity:UniqueID(); if (!timer.Exists(timerID)) then timer.Create(timerID, 12, 0, function() if (!IsValid(entity)) then timer.Remove(timerID); return; end if (!entity:Alive()) then timer.Remove(timerID); return; end if (entity:Health() > 10) then timer.Remove(timerID); return; end entity:SetHealth(entity:Health() - 1); if (entity:Health() <= 0) then entity:Kill(); timer.Remove(timerID); end end); end end end);
Thanks again for your script but it's really strange, I lose HP only when I have 9Hp or less. Shoud I write <=11 ?
I don't see anything that would cause that behavior.
Well I did that :     if (IsValid(entity) and entity:IsPlayer() and entity:Health() <= 11) then Now It's working as I want, I don't really know why but it's working
If you look in 's code, he assigns this variable for the timer name: local timerID = "fktimer" .. entity:UniqueID(); UniqueID accomplishes the same thing as SteamID in this regard. You could even use entity:EntIndex(). The SteamID isn't required, it just needs something to make the timer unique to other players.
Okay, thank you for clarifying !
It shouldn't behave like that, but I'm not at a computer where I can test it right now. Just roll with what works for you for now.
Yes sure Thank you for your help again.
Sorry, you need to Log In to post a reply to this thread.