Making a game that requires high numbers, how could I make a StringValue not turn negative as a leaderstat?
COMMUNITY QUESTION
How would one go about making stats not go negative?
3 answers
(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!
First off, I believe a StringValue can NOT go negative unless you do
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!
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
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
How is it becoming negative?
Are you referring to some sort of overflow?
Is there a reason for using a String value?
Have you tried using
math.max? http://wiki.roblox.com/index.php?title=Global_namespace/Mathematical_functions#math.max