Damage isn't working for whatever reason

Hey all, I’m looking for a solution to fix the damaging part of my hitbox code.

HitHumanoid:TakeDamage(Damage * Character.CombatFolder:WaitForChild("Statuses"):WaitForChild("Damage Multiplier").Value / Hit.Parent.CombatFolder:WaitForChild("Statuses"):WaitForChild("Damage Reduction").Value)

When both of these are set to 0, damage isn’t applied for some reason, so I’m looking for a fix.

I am assuming that you’re talking about the Value objects’ values when you say, “When both of these are set to 0…”

Anything multiplied by 0 is 0. And in this case, Damage is being multiplied by 0. And 0 divided by 0 is undefined. So setting a minimal value would help. You can use math.clamp() to automatically choose the minimal value over the value less than the minimal value when needed.

The first parameter is the number you’re trying to use, the second parameter is the minimal number and the third parameter is the maximal number. You’d want the lowest value possible to use to be 1; the highest value probably math.huge, unless you want to set a limit to the multiplier and reduction.

-- `multiplier` is the "Damage Multiplier" value;
-- `reduction` is the "Damage Reduction" value.

-- This is an example function and helps with readability.
local function clamp(value : number)
    return math.clamp(value, 1, math.huge)
end

-- If `multiplier` is lower than 1, it'll default to 1. And the same for `reduction`.
-- This will prevent multiplying and dividing a number by 0.
local outcomeDamage = damage * (clamp(multiplier) / clamp(reduction))

-- Damage the humanoid.
hitHumanoid:TakeDamage(outcomeDamage)

multiplier and reduction would have to both be 1 or more. But, obviously, the outcome can still be a decimal. For example, 1 divided by 4 is 0.25.
If you want the values to be able to be lower than 1 but still higher than 0, you can just adjust the clamp() function since it’s there anyway.

local function clamp(value : number)
    return (value > 0 and value) or 1
end

This (the code snippet above) would be more ideal if you don’t want the maximal value set, too.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.