# Need help with a health system in a game i'm making

**URL:** <https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687>\
**Category:** Coding Questions\
**Created:** [November 23, 2021, 4:45am UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687 "2021-11-23T04:45:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![UltimateDoug9](https://avatars.discourse-cdn.com/v4/letter/u/8491ac/32.png) [@UltimateDoug9](https://discourse.processing.org/u/UltimateDoug9)\
**Post date:** [November 23, 2021, 4:45am UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687/1 "2021-11-23T04:45:05Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147) Making a game of my computer studies class and i’m struggling to make a health system with just variables and if statement (that 's all were allowed to use).

code looks kinda like this…

float Ehp = 200; //enemy health

```
fill (0); //enemy health
rect (575, 500, 200, 40);
fill (0, 255, 0);
rect (575, 500, Ehp, 40);

if (y > 55 && y < 220)//yellow (does 50 damage)
{
  Ehp = 200 - 50;
}

if (y > 55 && y < 220 && Ehp == 150)//yellow - yellow
{
  Ehp = 200 - 100;
}

if (y > 90 && y < 187)//orange (does 100 damage)
{
  Ehp = 200 - 100;
}

if (y > 120 && y < 159)//red (does 150 damage)
{
  Ehp = 200 - 150;
}

if (y > 135 && y < 145)//black (does 200 damage)
{
  Ehp = 200 - 200;
}

```

Everything works except for when I want to shoot the enemy again yellow - yellow one won’t work because as soon as enemy’s health = 150, it instantly changes it to 100. I only want it to change to 100 after another shot is fired.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [November 23, 2021, 7:26am UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687/2 "2021-11-23T07:26:50Z")

</div>

From what I can see, it is because you have two IF statements one after another.  
If the first one is triggered, it fulfills the conditions to trigger the second, and it reduces Ehp again.  
You could solve that by either placing the second IF statement first OR add an ELSE IF after the first statement.

A simple example:

```auto
int a = 5;
if(a == 5) a --;
if(a == 4) a --;
println(a); //outputs 3.
a = 5;
if(a == 5) a --;
else if(a==4) a --;
println(a); //outputs 4

a = 5;
if(a==4) a --;
if(a==5) a --;
println(a); //outputs 4

```

---

<div class="post-metadata">

**Author:** ![UltimateDoug9](https://avatars.discourse-cdn.com/v4/letter/u/8491ac/32.png) [@UltimateDoug9](https://discourse.processing.org/u/UltimateDoug9)\
**Post date:** [November 23, 2021, 1:07pm UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687/3 "2021-11-23T13:07:46Z")

</div>

thanks. I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![UltimateDoug9](https://avatars.discourse-cdn.com/v4/letter/u/8491ac/32.png) [@UltimateDoug9](https://discourse.processing.org/u/UltimateDoug9)\
**Post date:** [November 23, 2021, 1:57pm UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687/4 "2021-11-23T13:57:31Z")

</div>

It didn’t seem to work. Any other ways?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [November 23, 2021, 6:02pm UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687/5 "2021-11-23T18:02:23Z")

</div>

Can you share the changes you made?

---

<div class="post-metadata">

**Author:** ![UltimateDoug9](https://avatars.discourse-cdn.com/v4/letter/u/8491ac/32.png) [@UltimateDoug9](https://discourse.processing.org/u/UltimateDoug9)\
**Post date:** [December 23, 2021, 11:23pm UTC](https://discourse.processing.org/t/need-help-with-a-health-system-in-a-game-im-making/33687/6 "2021-12-23T23:23:34Z")

</div>

I figured it out. thanks
