Attempting to award money per damage dealt reduces money instead

I’m working on a Tower Defense type game, and I’m struggling with a part of it. Whenever one of the towers I have placed down shoots an enemy, it will give the owner of the tower the amount of damage it dealt to the enemy. Problem is, if there are too many towers together and shooting at the same time, this amount can become negative, and reduce a lot of my money instead. How can I fix this issue?

Here is my code so far:

if plr then plr.Cash.Value += math.min(damage.Value, target.Humanoid.Health) end
target.Humanoid:TakeDamage(damage.Value)

negative values

1 Like

You can use math.clamp to prevent a value from going below a certain amount and above another amount.

(0 is the minimum amount, damage.Value is the maximum amount)

math.min(damage.Value, math.clamp(target.Humanoid.Health, 0, damage.Value))

Don’t use one-liners… please.

2 Likes

Your solution worked, thank you!

Sorry, lol.