# How would I go on making a Damage Resistance System?

Hey people so currently I’m struggling on making a damage reduction system that uses percentage and that doesnt affect a Humanoids MaxHealth, how would I go about this?

Heres what I’m saying; Player1 has %80 resistance, Player2 has %20 resistance, now if they both took 100 damage, how would I calculate that and change their health to that?

Tried looking everywhere for a solution, unfortunately I couldn’t find nothing.

I don’t have my main code with me, but I do have a old, crappy code I tried to use from a DevForum post: (this code was used on a NPC)

``````Local Humanoid = script.Parent.Humanoid
Humanoid.HealthChanged:Connect(function(Health)
if Health < CurrentHealth then
local Damage = CurrentHealth - Health
CurrentHealth = Health
local Amount = Damage/Resistance.Value
print(Amount)
Humanoid.Health -= Amount
end
end)
``````

I’m not asking for a whole code (although that would be nice) but I just need help on how to calculate the damage a humanoid took and make the resistance help out with it and make it take less damage of the percentage and give the Humanoid Health the resulf of it.

1 Like

Would this work?

``````local resistance = 80; -- // 80% resistance
local damage = 100; -- // 100 damage
local calculatedDamage = damage - damage*(resistance /100); -- // 80% resistance, 100 damage, would deal 20 HP.
humanoid.Health -= calculatedDamage
``````
2 Likes

There are 2 issues in your script:

• You’re not healing the humanoid back, before the damage calculation, you should add a
`Humanoid.Health = CurrentHealth`
• The damage calculation is incorrect, imagine the damage was 20 and your resistance was set to 80, that means the reduced damage would be 0.25. The proper formula is:
`Damage * Resistance / ResistanceMax`
Where ResistanceMax would be the maximum value of a player’s resistance. No idea what it would be for you, but I guess 100 should be enough. The division is done to restrict that value to 0 - 1.
2 Likes

Thank you, now how would I apply this to my block of code?

``````local Humanoid = script.Parent.Humanoid
PreviousHealth = Humanoid.Health

local function onHealthChanged()
if Humanoid.Health < PreviousHealth then
local Difference = PreviousHealth - Humanoid.Health
Humanoid.Health -= (Difference * Resistance)
print(Difference)
print("damage taken")
end

PreviousHealth = Humanoid.Health
end
``````

You’re going to have to put the

``````Humanoid.Health = PreviousHealth
``````

right after the line with “local Difference”
That heals the player back.

As for damage calculation, I’m not sure how you didn’t implement it yourself. It would be something like this:

``````Humanoid.Health -= Difference * Resistance / 100
``````

after the healing part line.

1 Like