Can somebody please explain to me what is happening in this code?

Hello there,

I need help explaining how does this code work.

Here is the code: (Credit for the Fortnite Verse documentations for this code):

if (PotionType = "damage"):
            if ((PlayerHealth > PotionDamageAmount)):
                set PlayerHealth = PlayerHealth - PotionDamageAmount
                Print("Full damage")
            else if (PlayerHealth > MinHealth):
                # Give player one more chance if their health is low
                set PlayerHealth = 1.0
                Print("PlayerHealth set to 1.0")
            else:
                set PlayerHealth = 0.0
                Print("Player eliminated!")

The reason why I need someone to explain this code for me is because there is one part of the code that I don’t understand and that part is this:

else if (PlayerHealth > MinHealth):
                # Give player one more chance if their health is low
                set PlayerHealth = 1.0
                Print("PlayerHealth set to 1.0")

That is the part that I’m confused about because from my understanding, it says right here that if PlayerHealth is bigger than MinHealth then, it would set the PlayerHealth to MinHealth and it would print PlayerHealth set to 1.0 and that’s the part that I don’t understand so can somebody please explain this code for me? Thank you.

Yeah, it seems like you’ve understood everything correctly. This is the logic of this particular poison mechanic.

@TZZonerPlayz if you’re still having issues understanding what the code is doing, it might help to assign values to PlayerHealth and PotionDamageAmount in your mind and then work through the control flow.

Imagine PlayerHealth is 24.0 and PotionDamageAmount is 25.0. This scenario should help you figure out the logic :slight_smile:

The code is used when the player is using a Damage Potion. So, the first step is checking if the Player Health is greater than the damage amount (so it doesn’t kill the player). If it is, it applies the full damage and the code is done.
The “else if” only applies if the player health is equal or lower the Damage Amount (only happens if the first part “failed”) and if it is greater than the MinHealth(1.0), so in order to not kill the player instantly, it sets his Health to 1.0. That’s why it sets his life to 1.0: His life was not big enought to receive the full damage, so instead of killing the player, it sets his Health to 1.0 to give him one more chance
Lastly, if the player’s health is not greater than the Damage Amount and not greater than the MinHealth (in other words, if his Health = 1.0), it will kill him

If that’s the case then, why does it say in the else if if the PlayerHealth is greater than MinHealth?

The function considers 3 different scenarios when the player takes damage:

  1. If the player’s health is greater than the damage amount, it will cause full damage (Example: player health is 80 and damage taken is 70. This will leave them with 10hp and the function will be complete).

  2. The second scenario only happens if the player’s health is not greater than the damage amount but is greater than MinHealth (which is 1, if I remember correctly). Taking the damage will set the player’s health to 1hp. (Example: player health is 50 and damage taken is 70. This would normally eliminate them, but in this case, it will set PlayerHealth to 1.0 to give them one last chance and avoid being eliminated too quickly).

  3. The last scenario will only happen if the player’s health is not greater than the damage amount and also not greater than MinHealth, which means they probably have 1HP. In this case, the damage will eliminate them.