So, if you didn’t know, if you reach a very high point of an int value, it resets to a very low number. Here’s the screenshot.
How do I fix it?
If ur game needs a very big number make it then 1, ,10 ,100, ,1K ,10K and so on so like the number of the thing in the leader board make it not int number make it string and make it like this u can search about the format number
Computers use fixed bits, and wrapping lets them keep counting efficiently without needing infinite storage.
Yeah, the highest value an IntValue can be is about 9 quintillion. If you need to have a higher number, use NumberValues instead.
What you’re observing is called integer overflow. IntValues store numbers as 64-bit signed integers, which have an upper limit of 2^63-1 (9.2 quintillion). As a consequence of two’s complement, exceeding this upper limit will cause its binary representation to flip to its negative lower limit, -2^63 (9.2 quintillion). Take this moment to reflect on how you managed to develop a game that can let users reach this absurdly large number. Should you need this to be possible, you’ll need to use a tool for representing numbers larger than 64 bits, such as BigNum
Well, this is common in simulators and games like that
How do I format the number into a shortened one? I’m new to leaderstats so I don’t have that much experience into it.
Large numbers are very common in the Incremental/Idler genres, and Googology-adjacent games. Although I do think tbat you should go into making an incremental with the knowledge of the limitations of Luau’s number types.
Save the number elsewhere, but display a string in leaderstats. I’m not at my computer right now but I think a function to format numbers will have something to do with the modulo operator and powers of 10.
Okay, so I made the script. But I have a problem. If I moved the number somewhere else, all of my earning scripts won’t work. So what do I do?
I don’t see how that could happen; can you share your scripts, what kind of script it is (client, server, module) and your hierarchy?
Your logic shouldn’t have changed, just what is shown to the player. (sorry if I worded my original response weirdly, not at my computer)
I also wont be able to respond for a while, though
That solved my problem. But I’m wanting to convert it into a small version because it takes up space if you have too much
Nice u formated it i have an advice for u the format u did right now u can makr a module script name it like number format the. In any script access it and put it in the replicated storage or ur modules folder then create the function for the formating for example
local formatNu = { —[[you can add setings here like formating to what like the letter that will be in the number]] — for example
Quadrillion = {
UniqeLetter = “Q”,
StartPoint = 1000000000000000 — when it will take that uniqe letter it will be lik 1Q
}
— you add the other here like K M B T Q…
}
function formatNu.formatNumber(Number) : String
— here u add rhe functionality of this
end
retuen formatNu
And now u can use it anywhere and u can using it in one line it will return like
Input
12500000
Output
12.5M —> string
Take this is full code
local FormatNumber = {}
FormatNumber.Settings = {
{ startNum = 1e12, keyLetter = "T" }, -- Trillions
{ startNum = 1e9, keyLetter = "B" }, -- Billions
{ startNum = 1e6, keyLetter = "M" }, -- Millions
{ startNum = 1e3, keyLetter = "K" }, -- Thousands
}
function FormatNumber.Format(num)
for _, setting in ipairs(FormatNumber.Settings) do
if num >= setting.startNum then
local value = num / setting.startNum
value = math.floor(value * 10) / 10
local str = tostring(value)
if str:sub(-2) == ".0" then
str = str:sub(1, -3)
end
return str .. setting.keyLetter
end
end
return tostring(num)
end
return FormatNumber
Example input and output
| FormatNumber.Format(500) → 500 |
| FormatNumber.Format(999) → 999 |
| FormatNumber.Format(1000) → 1K |
| FormatNumber.Format(1500) → 1.5K |
| FormatNumber.Format(1999) → 1.9K |
| FormatNumber.Format(1_000_000) → 1M |
| FormatNumber.Format(2_500_000) → 2.5M |
| FormatNumber.Format(1_200_000_000) → 1.2B |
And @Microwave_Toothpaste, I understand that, but even I would employ some restraint in how fast I would allow these values to increase
Unless I’m misunderstanding, shouldn’t that be as simple as formatting the number? I know ForeverSp4rK sent his implementation of a number formatter, but I personally don’t like how he implemented it—there’s no need to specify the startNum
for each abbreviation! Here’s mine:
local abbreviations = {"","K","M","B","T","Qa","Qi","Sx","Sp","Oc","No"} -- Add more here if needed
return function(num: number)
assert(type(num) == "number", "Input to number formatter must be a number")
if num == 0 then return "0" end
local oom = math.floor(math.log10(math.abs(num)) / 3)
return if oom < #abbreviations * 3 then
string.format("%.2f", num / (10 ^ (oom * 3))):gsub("%.0$", "") .. abbreviations[oom + 1]
else -- if the number is bigger than what the abbreviations support, use scientific
string.format("%.2e", num)
end
However, if by “takes up too much space” you meant in the Explorer, try out a library made for storing large numbers.
I don’t mean by explorer. I mean by this:
If it’s too small to see, it’s the money counter gui.
Okay, so it’s a visual problem, then, right?
Then you should abbreviate the number that gets shown to the player.
For instance, here’s some pseudocode:
local number = logicToGetTheNumber()
showNumberToPlayer(number)
That’s bad—that causes the problem you have.
So instead, format the number first.
local formatNumber = require("./path/to/number/formatter/module")
local number = logicToGetTheNumber()
showNumberToPlayer(formatNumber(number))
Now, instead of 348923048509...
we get something more manageable like 34.89Qa
So, I used the format number script and the formatter gave me an error. But for instance, here’s the leaderstats script.
local Players = game:GetService("Players")
local DataStoreService = game:GetServihttps://devforum.roblox.com/t/value-resets-to-a-very-low-number-when-value-increases-high-enough/3596268/20ce("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder", player)
folder.Name = "leaderstats"
local points = Instance.new("NumberValue", folder)
local addups = Instance.new("NumberValue", player)
local abreviatedpoints = Instance.new("StringValue", player)
addups.Name = "Addups"
addups.Parent = folder
addups.Value = 1
abreviatedpoints.Name = "Money"
abreviatedpoints.Value = ""
abreviatedpoints.Parent = folder
points.Name = "Interactions"
points.Value = 0
end)
Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)
if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)
local function Save(player)
local SavedData = {}
for _, v in pairs(player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end
local success, errormessage = pcall(function()
Saver:SetAsync(tostring(player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)
game.Players.PlayerAdded:Connect(function(player)
while wait() do
local formatNumber = require("./path/to/number/formatter/module")
local abreviated = player.leaderstats:WaitForChild("Money")
local points = player.leaderstats:WaitForChild("Interactions")
abreviated.Value = formatNumber(points.Value)
end
end)
This is the sign script
while wait() do
script.Parent.Text = "$".. game.Players.LocalPlayer.leaderstats.Money.Value
end
What’s the error? I tried it (the one that I wrote) and it seems to work well for me, unless I’m overlooking something.
I noticed in your code you have:
which is using the placeholder path. Make sure you’re changing that to the actual path of the module in your project
For instance, if your script is a sibling to the module, and the module is named NumberFormatter
,
local formatNumber = require("./NumberFormatter")