Upgrade price problem!

So i’m trying to make a formula for my price, but I can’t figure one out.

I’m trying to make it so that the first upgrade cost 100 and the second one gets multiplied by 2.2 and so on.

local price = 100 * player.Values.Upgrades.Value * 2.2

problem is that when the player begins the game the upgrades are 0 so it becomes

local price = 100 * 0 * 2.2

And I don’t want to make an ifstatement of 10 lines so can someone help me with this

1 Like

make the starting upgrade value to 1 or,

local price = 100 * (player.Values.Upgrades.Value + 1) * 2.2

this solution is kinda lazy but it works lol

1 Like

but it the player has 3 upgrades it will count as four?

1 Like
local price = 0

if player.Values.Upgrades.Value > 0 then
    price = 100 * player.Values.Upgrades.Value * 2.2
elseif player.Values.Upgrades.Value == 0 or player.Values.Upgrades.Value < 0 then
    price = 100 * 2.2
end
1 Like

didn’t you read my post fully?

1 Like

I am not sure I understand from your post whether you want it to be multiplied by 2.2, and then 2.2 again, or just have 2.2 multiplied by a number.

If you want to make it multiplied by a number, you can do that with an inline if statement.

local price = if(player.Values.Upgrades.Value == 0) then 100 else 100 * 2.2 * player.Values.Upgrades.Value

If you want to multiply it by powers of 2.2, that would be a geometric series, which means that the value a(n) = a1 * q^(n-1):

In code, a1 is the first term of the series, or in your case, the initial price. The q is the quotient of the geometric series, which in this case is your multiplier - 2.2, and n is the series current term, which in this case is your upgrades values.

In mathematic terms:
Price(upgrade) = initialPrice * (2.2)^(upgrades)

local price = 100 * (2.2)^(player.Values.Upgrades.Value)
1 Like
local multiplier = 2.2
local upgrades = player.Values.Upgrades.Value

local adjustedUpgrades = upgrades + 1

local price = basePrice * math.pow(multiplier, adjustedUpgrades - 1)
1 Like

Yes, that is true. But no need to add 1 to the adjustedPrices just to deduct 1 from it later. We can directly use upgrades

In addition, in Luau you can use the ^, are there any benefits to using the math.pow over using the ^ operator? (genuinely asking)
image

2 Likes

It’s the same, both achieve the same result, math.pow Is just more readable that’s all! :slight_smile:

2 Likes

If you really don’t want to use IF statements you can do the following,

If you are using tables to store data you can do this:
First create your table but don’t include the upgrade value

local data={
Upgradename="string",
UpgradeType="normal"
}

local function includekey(tbl)
tbl.UpgradeValue=1
return 1
end
 

then when you need to calculate the price of the upgrade you can do local price = 100*2.2*(data.upgradevalue or includekey(data)) so what happens is that you try to index a key that isn’t in the table so it returns nil and includekey will be called because of or and it will return 1 so it will be considered for the multiplication

If you are using Int Values you can do the same thing but you need to create another function,have upgrade intvalue as nil as in don’t create it yet and do a pcall like this

function createint(s,p)
 local a= Instance.new("IntValue")
 a.Parent=p a.Name=s 
return 1 
  end
local suc,val=pcall(function() return player.data.upgrade  )
local upgradeval= val.Value or createint("Upgrades",player)
local price = 100*2.2*upgradeval
1 Like

Tbh it feels like its easier to just use IF statements but if you dont like them you can do what i did

1 Like

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