Hi! I have rebirths in my game with different rebirth amounts. When you buy a rebirth, it increases by a little but. So buying 5 rebirths will not be 5x One rebirth, it will be the same amount as buying one rebirth five times and since the rebirth increases every time you buy it, the 5 rebirth final cost will be way more than one rebirth final cost. I use a for do loop to calculate this. However, when you get to something like a 15M+ Rebirth button, it lags a lot. How should I fix this?
local button = script.Parent
local plr = game.Players.LocalPlayer
local rebirthRE = game:GetService("ReplicatedStorage"):WaitForChild("rebirth")
local final_cost = 0
local buttonNum = 9
function roundNumber(num, numDecimalPlaces)
return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end
local abbreviations = {
"K", -- 4 digits
"M", -- 7 digits
"B", -- 10 digits
"T", -- 13 digits
"QD", -- 16 digits
"QT", -- 19 digits
"SXT", -- 22 digits
"SEPT", -- 25 digits
"OCT", -- 28 digits
"NON", -- 31 digits
"DEC", -- 34 digits
"UDEC", -- 37 digits
"DDEC", -- 40 digits
}
local function Abbreviate(x)
if x < 1000 then
return tostring(x)
end
local digits = math.floor(math.log10(x)) + 1
local index = math.min(#abbreviations, math.floor((digits - 1) / 3))
local front = x / (math.pow(10, index * 3))
local Front = roundNumber(front, 2)
return Front .."".. abbreviations[index] .."+"
end
local function createFinalCost(num)
final_cost += (plr:WaitForChild("leaderstats"):WaitForChild("Rebirths").Value + num) * 50
end
for i = 1, script.Parent.Parent.RebirthAmount.Value do
createFinalCost(i)
end
script.Parent.Parent.CrownAmount.Text = (Abbreviate(final_cost)) .." Crowns"
if plr:WaitForChild("RebirthButtonAmount").Value >= buttonNum then
script.Parent.Lock.Visible = false
end
button.MouseButton1Click:Connect(function()
if script.Parent.Lock.Visible == false then
if plr:WaitForChild("leaderstats"):WaitForChild("Crowns").Value >= final_cost then
rebirthRE:FireServer(final_cost, script.Parent.Parent.RebirthAmount.Value)
end
end
end)
plr:WaitForChild("leaderstats"):WaitForChild("Rebirths").Changed:Connect(function()
final_cost = 0
for i = 1, script.Parent.Parent.RebirthAmount.Value do
createFinalCost(i)
end
script.Parent.Parent.CrownAmount.Text = (Abbreviate(final_cost)) .." Crowns"
end)
plr:WaitForChild("RebirthButtonAmount").Changed:Connect(function()
if plr:WaitForChild("RebirthButtonAmount").Value >= buttonNum then
script.Parent.Lock.Visible = false
end
end)