Im not getting it to work, its show up as this
In this image i have 16 cash.
Im not as advanced so most posts use math.
local abbrev = {
["A"] = 1e3,
["B"] = 1e6,
["C"] = 1e9,
["D"] = 1e12,
["E"] = 1e15,
["F"] = 1e18,
["G"] = 1e21,
}
game.Players.LocalPlayer.leaderstats.money.Changed:Connect(function()
local money = game.Players.LocalPlayer.leaderstats.money.Value
for number,requirement in pairs(abbrev) do
if requirement >= money then
script.Parent.Text = money/requirement .. number
end
if requirement <= money then
script.Parent.Text = money
end
end
end)
Edit: I added more numbers to the abbrev table, and it always goes to the second last.
So if i go to z it will go to y
Okay so I see a few issues with this code, ill explain my thought process and how to identify and fix them yourself in the future
First of all a notion to be aware of; dictionaries like abbrev will be looped over in a random order, i.e. you cannot be sure if A = 1e3 or F = 1e18 will be looped over first.
You seem to have swapped the arguments of the greater than statement around, assuming the abbreviation should only appear if the player has that amount of money or more. i.e. the following pseudo code:
if money > requirement then
--apply abbreviation
end
The bug you described is probably a result of both oversights stated above
So working towards a solution;
Will make two lists so we can guarantee the order in which requirements are checked,
Will be assuming if a higher requirement is met it should supersede the lower requirement
Will flip the greater than statement
-- first element of requirement corresponds to first element of abbreviation etc.
local requirement = {1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21}
local abbreviation = {"A", "B", "C", "D", "E", "F", "G"}
if #requirement ~= #abbreviation then
error("Sanity: abbreviation and requirement should have same amount of elements")
end
local function truncate(money)
-- ensures the money + abbreviation is at max 5 characters
-- i.e. 21_300 -> 21.3A
-- i.e. 110_000 -> 110A
local max_length = string.sub(tostring(money), 1, 4)
return tonumber(max_length) -- tonumber removes any trailing periods (e.g. 100. becomes 100)
end
game.Players.LocalPlayer.leaderstats.money.Changed:Connect(function(new_value)
local chosen_abb = ""
local chosen_req = 1
local money = new_value
-- using ipairs guarantees predictability for the order of iteration
for i, req in ipairs(requirement) do
if money >= req then
chosen_req = req
chosen_abb = abbreviation[i] -- corresponding abbreviation for this requirement
end
end
local money_display = tostring(truncate(money/chosen_req))..chosen_abb
script.Parent.Text = money_display
end)
I hope that helped, if you have any questions feel free to reach out.
Kind Regards.