I am making a gui display for the players coin value, I’m using a .changed event to change the value of the ui so it stays up to date with the players real currency value. Recently I added some code to abbreviate the displayed number, but now the .changed function doesn’t work. Also, sometimes the gui displays 0, making you have to rejoin to fix the error.
This is the code for my gui:
local AbbreviateNumber = require(game.ReplicatedStorage:WaitForChild("AbbreviateNumber"))
local coinBalance = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local Coins = leaderstats:WaitForChild("Coins")
local number = Coins.Value
local text = AbbreviateNumber:Abbreviate(number)
coinBalance.Text = text
Coins.changed:Connect(function()
coinBalance.Text = text
end)
and this is the code which the module script uses to abbreviate the value:
local AbbreviateNumber = {}
local abbreviations = {
K = 4,
M = 7,
B = 10,
T = 13
}
function AbbreviateNumber:Abbreviate(number)
local text = tostring(math.floor(number))
local chosenAbbreviation
for abbreviation, digits in pairs(abbreviations) do
if #text >= digits and #text < (digits + 3) then
chosenAbbreviation = abbreviation
break
end
end
if chosenAbbreviation then
local digits = abbreviations[chosenAbbreviation]
local rounded = math.floor(number/10 ^ (digits - 2)) * 10 ^ (digits - 2)
text = "$"..string.format("%.1f", rounded/ 10 ^ (digits - 1)).. chosenAbbreviation
else
text = "$"..number
end
return text
end
return AbbreviateNumber