Abbreviation System Error

This abbreviation system supports all the abbreviations listed but infinite.

Error:

local Abbreviations = {
	"";
	"K";
	"M";
	"B";
	"T";
	"Q";
	"Qx";
	"Sx";
	"Sp";
	"Oc";
	"N";
	"D";
	
	"∞";
}

local function AbbreviateNumber(Number)
	for i = 1, #Abbreviations do
		if Number < 10 ^ (i * 3) then
			if Abbreviations[i] == "∞" then
				return "∞"
			else
				return math.floor(Number / ((10 ^ ((i-1) * 3)) / 100)) / (100) .. Abbreviations[i]
			end
		elseif tostring(Number) == "inf" then
			return "∞"
		end
	end
end

while wait() do
	local Player = game.Players.LocalPlayer
	script.Parent.Text.Text = "🍬 Candy: "..AbbreviateNumber(1000000000000000000000000000000000000000) -- Placeholder number
end
1 Like

Looking at the error code, it’s saying that you’re trying to join a number with [nil]. On the line

script.Parent.Text.Text = "🍬 Candy: "..AbbreviateNumber(1000000000000000000000000000000000000000)

your trying to join the number (1000000000000000000000000000000000000000) and nil, which is returned by the abbreviatenumber function. So, your problem is lying in the abbreviatenumber function. To debug, try replacing this line before any return's in your code, replace:

return "∞"

with:

print("Trying to return infinity")
return "∞"

Alright I’ll attempt debugging it with this.

Okay so I ran a similar test and I found this though I’m not sure what the bug is, I’ve narrowed it down to this line of code.

local function AbbreviateNumber(Number)
	print("Test 1")
	for i = 1, #Abbreviations do
		print("Test 2")
		if Number < 10 ^ (i * 3) then
			print("Test 3")
			if Abbreviations[i] == "∞" then
				print("Test 4")
				return "∞"
			else
				return math.floor(Number / ((10 ^ ((i-1) * 3)) / 100)) / (100) .. Abbreviations[i]
			end
		elseif tostring(Number) == "inf" then
			return "∞"
		end
	end
end

This is the part that is giving error, try focusing on this and maybe you will find the solution.
What I mean was, try to remove that.

local Player = game.Players.LocalPlayer
script.Parent.Text.Text = "🍬 Candy: "..AbbreviateNumber(1000000000000000000000000000000000000000) -- Placeholder number

I’m not really sure what you mean as that number was a placeholder to see if it would work if the value was that. This is the actual code.

while wait() do
	local Player = game.Players.LocalPlayer
	script.Parent.Text.Text = "🍬 Candy: "..AbbreviateNumber(Player:WaitForChild("leaderstats"):WaitForChild("🍬 Candy").Value)
end

Based on your actual code, I can literally see the error.

local Player = game.Players.LocalPlayer
local NumberValue = Player:WaitForChild("leaderstats"):WaitForChild("🍬 Candy")

script.Parent.Text.Text = "🍬 Candy: "..AbbreviateNumber(NumberValue.Value)
	
NumberValue.Changed:Connect(function()
    script.Parent.Text.Text = "🍬 Candy: "..AbbreviateNumber(NumberValue.Value)
end)

Hope this helps!

1 Like

Alright I’ll test this solution.

Okay so I found a way to play test it, though when the candy value gets too high it resets back to 0.