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
1 | if n > 1000 then |
2 | return math.floor(n / 1000 ) .. "K" |
3 | elseif n > 1000000 then |
4 | return math.floor(n / 1000000 ) .. "M" |
5 | else |
6 | return n .. "" |
7 | end |
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?
If you store the number in a IntValue object then you can create a listener for when the IntValues value changes
01 | local intvalue = Instance.new( "IntValue" ) |
02 |
03 | scoreboardCalculations = 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 |
13 | end |
14 |
15 | intvalue.Changed:Connect(scoreboardCalculations) |
The code might need a little cleaning up i'm working off a crappy tablet Hope this helped
You can convert the value to string then use the length of the string to get the correct unit.
Example
01 | local unitTable = { "K" , "M" } -- unit list |
02 |
03 | local 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) |
14 | end |
15 |
16 | for i = 0 , 1000000 , 100 do |
17 | print (formatNumber(i)) |
18 | end |
Hope this helps.