Unlimited rebirths formula

local IndexValue = RebirthDataValue["Rebirths"] -- How many rebirths you want
local FirstPrice = 25 + (Rebirths * 0.5) -- Basic price of one rebirth based on your rebirths
local LastPrice = FirstPrice + (IndexValue * 0.5) -- Price of many rebirths
			
local Price 
if IndexValue == 1 then
	Price = math.floor(FirstPrice)
else
	Price = math.floor((IndexValue/2) * (FirstPrice + LastPrice))
end

So I got this formula for rebirths but I wanted to add an “unlimited rebirths” option, it would calculate how many rebirths you can do based on your actual money… but I don’t know how to do that. As you can see, the price updates after every rebirth, that means that if you have 5 rebirths and you wanna rebirth once, it would actually cost 26-27 compared to if you had 0 rebirths and it would cost 25 only.

2 Likes

What is this variable referring to exactly? If it is just a linear cost scaling then there is a nice maths formula you can use.

This refers to how many rebirths you currently have, sorry hehe :sweat:

1 Like

Okay so the cost of your rebirths seems to follow what is known as an arithmetic sequence. There are three variables that are used to describe an arithmetic sequence:

  • a_1 – the first value
  • d – the common difference
  • n – the index

Currently you have a_1 = 25, d = 0.5, n = IndexValue.

The relevant formula for this comes from the summing over some range of the sequence,
S_n = (n/2) * (a_1 + a_n).

This formula is actually used to give this line in your code:
Price = math.floor((IndexValue/2) * (FirstPrice + LastPrice))
We can re-arrange this to figure out IndexValue (how many rebirths you can buy) based on Price (how much money you have to spend). I’ll ignore the math.floor call for now (we can see if that causes issues later).

As LastPrice = FirstPrice + (IndexValue * 0.5) we need to substitute this into the formula in order to isolate IndexValue fully:

Price = (IndexValue/2) * (FirstPrice + FirstPrice + IndexValue * 0.5)
Price = IndexValue * (FirstPrice + (1/4) * IndexValue)

After rearranging, it becomes:

IndexValue^2 + 4*FirstPrice *IndexValue - 4*Price = 0.

Whack out the quadratic formula to get:

IndexValue = 2*(math.sqrt(FirstPrice^2 + Price) - FirstPrice)

Which may not be an integer so at this point it makes sense to apply a math.floor call to wrap around.

Which gives the final formula that should be all you need:

IndexValue = math.floor(2*(math.sqrt(FirstPrice^2 + Price) - FirstPrice))

Edit: FirstPlaceFirstPrice

1 Like

Holy, you’re totally right! I’ve just noticed it was an arithmetic sequence. Thank you for the detailed explanations too, the formula works really nicely O:

1 Like

Happy to help. It is worth checking with the initial Price formula to make sure they agree with each other. The blatant disregard for the math.floor call may mean they differ for really large IndexValues