Script returning zeroes

Hello! I have a script that abbreviates the number! However, it returns as zero why? I’ve added some comments that I’ve printed. Please help!

local BallFrame = script.Parent
local RS = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local TS = game:GetService("TweenService")
local info = TweenInfo.new(.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local GrantMP = RS:WaitForChild("GrantMP") -- Event
local Cost = RS:WaitForChild("Cost") -- Event

local BuyUpgrade = BallFrame:GetChildren()

local Divisible = 1.3 -- Divible by Offset
local Upg = 1.5

local Abbreviations = {
	"";
	"K";
	"M";
	"B";
	"T";
	"q";
	"Q";
	"s";
	"S";
	"o";
	"n";
	"d";
	"U";
	"D";
	"Tr";
	"Qt";
	"Qd";
	"Sd";
	"St";
	"Oct";
	"Nod";
	"Vg";
	"Uvg";
	"Dvg";
	"e70";
	"e71";
	"Tvg";
	"e73";
	"e74";
	"Qavg";
	"e76";
	"e77";
	"Qivg";
	"e79";
	"e80";
	"Sxvg";
	"e82";
	"e83";
	"Spvg";
	"e85";
	"e86";
	"ocvg"

}

local function Number_Abbreviations(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


for i, Button in pairs(BuyUpgrade) do
	if Button:IsA("TextButton") then
		local ButtonX = Button.Size.X
		local ButtonY = Button.Size.Y
		
		Button.MouseButton1Down:Connect(function()
			if plr.leaderstats.Balloras.Value >= Button.Cost.Value then
				
				Cost:FireServer(Button.Cost.Value)
				GrantMP:FireServer(Button.RewardMP.Value)
				local Calculation = math.round(plr.MP.Value * Upg)
				Button.Cost.Value *= Calculation -- Returns Zero and Button.Cost.Value is 10
				
				
				Button.Text = Button.Upgrade.Value.." "..tostring(Number_Abbreviations(Button.Cost.Value)).."$"
			end
			
		end)
		Button.MouseEnter:Connect(function()
			TS:Create(Button, info, {Size = UDim2.new(Button.Size.X.Scale, Button.Size.X.Offset/Divisible, Button.Size.Y.Scale, Button.Size.Y.Offset/Divisible)}):Play()
		end)
		Button.MouseLeave:Connect(function()
			TS:Create(Button, info, {Size = UDim2.new(Button.Size.X.Scale, ButtonX.Offset, Button.Size.Y.Scale, ButtonY.Offset)}):Play()
			
		end)
		
		while true do
			local Calculation = math.round(plr.MP.Value * Upg)
			print("This is "..Calculation) -- Returns the Calculations (Works)
			
			Button.Cost.Value *= Calculation -- Button.Cost.Value is 10
			print(Button.Cost.Value) -- Return also zero but why?
			Button.Text = Button.Upgrade.Value.." "..tostring(Number_Abbreviations(Button.Cost.Value)).."$" -- Returns value as zero?
			task.wait(1)
		end
		
	end
	
end
1 Like

You should print Calculation here after it is declared, and see if it is zero. Error might have to do with plr.MP.Value or Upg

Button.MouseButton1Down:Connect(function()
			if plr.leaderstats.Balloras.Value >= Button.Cost.Value then
				
				Cost:FireServer(Button.Cost.Value)
				GrantMP:FireServer(Button.RewardMP.Value)
				local Calculation = math.round(plr.MP.Value * Upg)
				Button.Cost.Value *= Calculation -- Returns Zero and Button.Cost.Value is 10
				
				
				Button.Text = Button.Upgrade.Value.." "..tostring(Number_Abbreviations(Button.Cost.Value)).."$"
			end
			
		end)
2 Likes

The problem isn’t the calculations. It’s the button.cost because after I multiply it. It returns a zero which shouldn’t be happening and this is stopping me from progressing.

Could it be multiplying by zero somewhere, thus keeping it equal to zero the whole time because it is constantly multiplying a zero factor?

Or have you already checked that?

Yes I’ve checked the Cost value, printed calculations which does not return a zero. It’s annoying and frustrating.

The only suspicious thing is this:

Are you sure that

  1. Button.Cost is a NumberValue?
  2. Button.Cost is not 0 (this is a pretty obvious one, but just to be sure)?

Some other stuff not related to your issue:

  • You should learn about ModuleScripts, and separate the code for abbreviating the numbers into one to reduce clutter in this main script, which seems to handle displaying the costs of upgrades.

You can simplify this to

for i, Button in BuyUpgrade do

, because it means the same thing.

Roblox has a string interpolation feature that lets you simplify the above down to the following:

print(`This is {Calculation}`)

(It uses backticks [`] and not quotation marks [" or '], and curly braces [{ and }] to place the value in the string…)

Double yes. I used to use intvalues but I switched to number values because it supported decimals.