Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Leaderstat Rounding Help 2 !?!

Asked by 9 years ago

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
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. NotsoPenguin 705 — 9y
0
How would I fix that? attackonkyojin 135 — 9y

1 answer

Log in to vote
4
Answered by
Unclear 1776 Moderation Voter
9 years ago

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)

Ad

Answer this question