What im going for is a custom Explosion that damages both players, and destructible blocks based on distance, but only after a certain point which is controlled by a “Dropoff Value”, hence the “advanced” part. I drew some crude graphs to demonstrate what I mean below.
Distance represents the explosion’s BlastRadius with 0% being the epicenter, and 100% being on its very edge. While Damage is just the base DamageValue.
For the past while ive been using the default equation listed on the Roblox Creator Hub which is listed below:
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
end
My version however was slightly modified to include a Dropoff Value which is listed below:
local DISTANCE_FACTOR = PART_DISTANCE / NEW_EXPLOSION.BlastRadius -- Get The Distance As A Value Between 0 And 1 For Dropoff Effects.
DISTANCE_FACTOR = 1 -DISTANCE_FACTOR -- Flip The Amount, So That Lower == Closer == More Damage.
if DISTANCE_FACTOR > DROPOFF_VALUE then -- Deal Full Damage.
HUMANOID:TakeDamage(MAX_DAMAGE)
elseif DISTANCE_FACTOR <= DROPOFF_VALUE then -- Deal Partial Damage Based On Distance.
HUMANOID:TakeDamage(MAX_DAMAGE * (DISTANCE_FACTOR / DROPOFF_VALUE))
end
My problem persists even when I remove the modifications and use the original equation however, so I don’t believe that to be the issue.
Basically while the equation works for the most part, i noticed while testing that sometimes the maxDamage * distanceFactor equation returns a negative number (usually near the edge of the BlastRadius) which ends up healing the player or block instead of damaging them. It seems to be mostly random, but significantly more common when maxDamage is much higher then the player or block’s MaxHealth. That’s not always the case though.
Ive played around with it and haven’t been able to solve the issue. For now I just use math.clamp() to prevent negative numbers from appearing and healing the player or block, but that still means a zero will return and players/blocks just wont take any damage when they should be.
How do I properly calculate damage dropoff like how I demonstrated in my graphs? Any help would be greatly appreciated!