Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Leaderstat Rounding Help 2 !?!

I just made my mana class from NumberValue into IntValue, and now the regen script broke?

New Regen script with round

P = script.Parent.Parent

local target = script.Parent.Parent
local manaValue = target.bin.Mana

while true do
    wait(1)
    local newValue = manaValue.Value + (manaValue.Value / 120)
    manaValue.Value = math.floor(newValue + 0.5)
end

Old Regen script

while true do
    wait(1)
 P.bin.Mana.Value = P.bin.Mana.Value + (P.bin.MaxMana.Value / 120)
    end

2 comments

1

The problem is that if the original mana value is small enough it will be increased but then you are rounding it back down to the original. So the value never changes. You should just keep the mana separate from the leaderstat.

0

How would I fix that?

1 answer

✓ Accepted answer

What is happening is your entire calculation is being rounded. This means that your calculation will be pointless if manaValue.Value / 120 is less than 1, since it will be rounded down and be equivalent to no change at all!

We can fix this by just rounding up every time, so we still notice an increase in mana every second rather than waiting for manaValue.Value / 120 to go above 1.

Also, since this is mana regeneration, I am guessing that you want to cap it at the original mana value. We will need to add additional code to do this, and we can simply just check if the new calculation will pass the original mana value and correct it accordingly.

Here is what that would look like...

local target = script.Parent.Parent
local manaValue = target.bin.Mana

local originalMana = manaValue.Value

manaValue.Changed:connect(function(value)
    if value < originalMana then
        local result = value + math.ceil(value / 120)
        if result > originalMana then
            result = originalMana
        end
        manaValue.Value = result
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.