Doubt with my damage system

I am creating a script that applies damage to the player but I have a problem.

the player may or may not have a shield which is stored in an intvalue I am applying the damage to the shield but if the damage exceeds the amount of the shield the value becomes negative

How do I get it to detect when the shield reaches 0 and apply the remaining damage to the player’s life?

I know that I have to apply the damage progressively to be able to detect when it reaches 0 but I am worried that using a for loop will delay the game

What do you advise me?

3 Likes

All you need to do is when the damage event runs check if the shield is less than or = to 0 and if it is then instead of damaging the shield, you would damage the character, for example,

-- you would put this in your damage script, so everytime the character gets damage, it checks if the shield is at 0 or less. 
if Shield.Value <= 0 then
Humanoid:TakeDamage(--Amount)
end

I tried but the problem is when the damage is 150 and the shield is 100 the shield is at -50

I want to avoid that

1 Like

Ohm okay then all you need to do is add this tiny part

if Shield.Value <= 0 then
Shield.Value = 0 
Humanoid:TakeDamage(--Amount)
end

This will make it so if the shield goes to the negatives it goes back to 0

1 Like
--never tested this out but maybe it works
humanoid:TakeDamage(math.max(0, damageTaken-shield.Value))
shield.Value = math.max(0, shield.Value-damageTaken)

I will try these 2 options and let you know if they work for me