I think my script is broken? But not sure

Hello, I’ve coded a function in the game with the help of someone in the forum that will calculate the cost of an upgrade and then display the next benefits for each upgrade… but for some reason instead of displaying ‘1.85,1.90,1.95’ etc … its displaying ‘1.85000000000000000001’ and i honestly have no idea what its causing this issue, here is a screenshot of the in-game GUI…

Here’s the GUI code…

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local MultiplierFolder = player:WaitForChild("MultiplierFolder")
local Multiplier1 = MultiplierFolder:WaitForChild("Multiplier1")

local ScreenGui = script.Parent
local MainFrame = ScreenGui:WaitForChild("MainFrame")
local NextPrice = MainFrame:WaitForChild("NextPrice")
local CurrentMulti = MainFrame:WaitForChild("CurrentMulti")
local NextMulti = MainFrame:WaitForChild("NextMulti")
local CloseButton = MainFrame:WaitForChild("CloseButton")
local UpgradeButton = MainFrame:WaitForChild("UpgradeButton")


local Remotes = ReplicatedStorage.Remotes
local Event = Remotes:WaitForChild('UpgraderClient')
local EventSend = Remotes:WaitForChild('UpgradeFinal')

---

local currentNumber = Multiplier1.Value
local baseCost = 50000
local costMultiplier = 1.25

---

local formatNumber = (function (n)
	n = tostring(n)
	return n:reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
end)

local function calculateUpgradeCost(currentNumber)
	return math.floor( baseCost * (costMultiplier ^ currentNumber) )
end

---

local Cost = calculateUpgradeCost(currentNumber)

local function setInitialValues()
	NextPrice.Text = '$'.. formatNumber(Cost)
	CurrentMulti.Text = 'X '.. Multiplier1.Value
	if Multiplier1.Value >= 2 then
		NextMulti.Text = 'MAX'
	elseif Multiplier1.Value <= 1.95 then
		NextMulti.Text = 'X ' .. Multiplier1.Value + 0.05
	end
end
setInitialValues()

---

Multiplier1.Changed:Connect(function(nextval)
	Cost = calculateUpgradeCost(nextval)

	NextPrice.Text = '$'.. formatNumber(Cost)
	CurrentMulti.Text = 'X '.. Multiplier1.Value
	if Multiplier1.Value >= 2 then
		NextMulti.Text = 'MAX'
	elseif Multiplier1.Value <= 1.95 then
		NextMulti.Text = 'X ' .. Multiplier1.Value + 0.05
	end
end)

UpgradeButton.Activated:Connect(function()
	if Multiplier1.Value == 2 then return end
	local valToTake = EventSend:FireServer(player)
end)


MainFrame.CloseButton.Activated:Connect(function()
	ScreenGui.Enabled = false
end)

Event.OnClientEvent:Connect(function()
	ScreenGui.Enabled = true
end)

it’s not broken just an issue with how binary values are converted to numbers as a quick fix just multiply by 100 then floor it then divide by 100.

math.floor(Multiplier1.Value + 0.05) * 100 ^ 100

so this should technically work?
Because this is what I’m getting

You just have to reformat the string so they represent without showing those floating-point errors. Strings do that the best way without incurring any weird numerical operations that can result in floating-point errors.

string.format("%.2f", inputNumbers)

1 Like

definately not, the ^ operator is raising that to the power of 100. What he means is

math.floor(Multiplier1.Value * 100) / 100

Your issue before is the common issue of :star2: floating point numbers​:star2:
Its ultimately just a result of binary not being able to represent certain things

value *= 100
value = math.floor(value)
value /= 100

--Replace value with the value you wanted

Try this instead

yeah sorry, i forgot ^ isn’t a dividing, but it sort of fixed it, but it sort of goes back to normal after a few upgrades…
https://gyazo.com/085f67adc05d2dd1d94452b4149041e9

I tried ur method, and it works! thank you very much @anon81993163

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.