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

How would one go about making stats not go negative?

Asked by 6 years ago

Making a game that requires high numbers, how could I make a StringValue not turn negative as a leaderstat?

0
How is it becoming negative? TheFierceWaffle 64 — 6y
0
Are you referring to some sort of overflow? fredfishy 833 — 6y
0
Is there a reason for using a String value? NinjaManChase 226 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

(Assuming you converted your stringvalue to an intvalue,)

You could use

intValue.Changed:Connect(function(val)
    intValue.Value = math.abs(val) == val and val or 0
end)

However, you should always check if it is negative before assigning a value to the stat.

Hope this helps!

Ad
Log in to vote
0
Answered by 6 years ago

First off, I believe a StringValue can NOT go negative unless you do

lua local Number = tonumber('-1')

But:

If it was a intvalue you can used .Changed event to detect change and if it goes below 0 then change it back to 0:

local IntValue = Instance.new('IntValue')
IntValue.Value = 0

IntValue.Changed:Connect(function()
    if IntValue.Value < 0 then
        IntValue.Value = 0
    end
end)

This will prevent the value going below 0. Hopefully, this helped. Good luck!

Log in to vote
0
Answered by 6 years ago

Whenever you update the stat, you can use math.clamp to clamp the value between a minimum and a maximum. Example:

local currentStat = 0
local intvalue = workspace.IntValue
function updateStat(stat)
    local newStat = math.clamp(stat, 0, 9999999999)
    intvalue.Value = newStat
end

while true do
    wait(1)
    updateStat(currentStat + 1)
end

Answer this question