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

How can you deal with large numbers?

Asked by 6 years ago

Hello, i have the following issue.

my game allows player to get a very large score having 10 digits in the leader board looks ridicules so i came up with the following solution

1if n > 1000 then
2    return math.floor(n / 1000) .. "K"
3elseif n > 1000000 then
4    return math.floor(n / 1000000) .. "M"
5else
6    return n .. ""
7end

the issue is where should i place the statement?

should i make it into a function then call it every time a script changes to score value? or is there a way to run something everytime a value is changed?

0
You can make a NumberValue (an actual physical object) that contains the player's score and use the the Changed event on that. I don't really use dataStore or leaderboards much so I couldn't tell you exacty where to store the NumberValue, that's up to you. Trew86 175 — 6y
0
Slightly off topic but you need to check for n > 1000000 first instead of n > 1000 because if the score is 100000000 for example it still uses K instead of M. Rheines 661 — 6y

2 answers

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

If you store the number in a IntValue object then you can create a listener for when the IntValues value changes

01local intvalue = Instance.new("IntValue")
02 
03scoreboardCalculations = function()
04 
05    if n > 1000 then
06        return math.floor(n / 1000) .. "K"
07    elseif n > 1000000 then
08        return math.floor(n / 1000000) .. "M"
09    else
10        return n .. ""
11    end
12 
13end
14 
15intvalue.Changed:Connect(scoreboardCalculations)

The code might need a little cleaning up i'm working off a crappy tablet Hope this helped

0
is Instance.new not Instance.New yHasteeD 1819 — 6y
0
intvalue.Changed:Connect(scoreboardCalculations()) is running the function not passing the function to the Changed event User#5423 17 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You can convert the value to string then use the length of the string to get the correct unit.

Example

01local unitTable = {"K","M"} -- unit list
02 
03local function formatNumber(val)
04    -- count the string length take 1 to give us the base 3 count
05    -- math.floor is the same as v - (v % 1)
06    local unit = math.floor((#tostring(val) - 1) / 3)
07 
08    if unitTable[unit] then
09        -- return a string with the correct using using ^ for devision
10        return tostring(math.floor( val / (1000 ^ unit))) .. unitTable[unit]
11    end
12 
13    return tostring(val)
14end
15 
16for i=0, 1000000, 100 do
17    print(formatNumber(i))
18end

Hope this helps.

0
^ ScriptGuider 5640 — 6y

Answer this question