Rebirth math problem

Hello! I would like to make a rebirth system for my game but I don’t know how to calculate the after-rebirth price while not rebirthing for huge amounts.

Let me explain. Okay, so, you have 1 rebirth. Each rebirth costs your actual rebirth * 10k in this case. So, if you had 2 rebirths, it would cost 20k, 3 rebirths-30k etc.

Now, what I want is to make massive rebirths directly, since rebirthing makes you lose everything you have. So like, I want into the process of finding price, if player chose to rebrith 5 times, it’ll count like that :

local RebirthsIHave = 1

local HowManyRebirths = 5
local price = 0

for i = 1, HowManyRebirths do
    price = price + RebirthsIHave * 10000 -- The price will increase as we rebirth
    RebirthsIHave = RebirthsIHave + 1
end

This would work but I don’t want to use a for loop, I want to get it done instantly, calculating the price only 1 time, meaning it will also calculate the price of the next rebirth until 5 in this case.

1 Like

This should do the trick but it’s quite complicated. Sorry if I make a mistake within this code, I’m not used to coding on my phone.

local RebirthsIHave = 1
local HowManyRebirths = 5
local Min = RebirthsIHave + 1
local Max = RebirthsIHave + HowManyRebirths

local price = HowManyRebirths % 2 == 0 and ((Min + Max) *  HowManyRebirths/2) * 10000 or ((Min + Max) *  ((HowManyRebirths  + 1)/2) - (Min + Max)/2) * 10000
print(price) —> 200000 

I felt like looping method is a lot easier than this one though.

1 Like