[Open City Game] I need a function!

Hello Forum!
Today I’m here to ask for a formula.
Yes, I can write the scripts, but without the maths, it’s not possible.
What I need
So, we’re a open city game, and in order to make the game realistic, players will lose bounty and/or money upon death or arrest.
Now, you might think that this is a simple code, but nope. Carry on reading :arrow_down_small:
We can’t just take away 10% or 50% of the player’s balance, what if he have millions? Billions? Trillions?
For example, let’s use the names Bob and Alex, if Bob had 10$ and died, I think it would be appropriete to take 9$ from him. If Alex had 100 Billion $ and died, I do not think it would be appropriete to take 90 Billion $ from him, but something like 1.5 Billion $ instead.
So, I tried to make many separate formula, for example When player dies, if balance = 10< ? <100, then he’ll lose 80%, if balance = 1000000 < ? < 10000000, then he’ll lose 3%.
Sounds cool, but it’s not good enough. Not precise enough.
I need a function ,
Balance x function = Percentage taken away.
If balance high = percentage low = A
If balance low = percentage high = B
A > B

(Please only post useful comments ;P)


What about letting them store their balance safely in some kind of bank, and making them lose a percentage only of the balance they have withdrawn? That way, if they had grinded millions, they would be able to safely store them.
This of course wouldn´t work in every game, maybe you don`t want this system, but just a suggestion…

1 Like

Ok thanks! That I’ll keep in mind. The special thing with my bank is, there’s a bank tax, but also a profit system. So basically more money + longer stayed in bank = more profit.
Still I would need a formula :stuck_out_tongue:
LoL I found a pun Forumula

1 Like

I just thought if this, its not the best but:

money = math.floor(money/(#string.format("%.f",money)+1))

money is divided by how many digits there are plus 1.
eg: money = 1000

money = 1000
money = 1000 / (#tostring(money)+1)
money = 1000 / (4+1)
money = 1000 / 5
money = 200

or if you want, you can probably subtract that from the original money

money = 1000 - 200
money = 800

Mhm, smart, I’ll check if it’s logical. Thanks

Your idea may drive players away from the game. (If they keep dying and can’t get anywhere, or someone is hacking and making them lose all their money, it may end up with your game not being successful.)

Anyway, I made this system to take a completely random amount from them based on their amount of currency.

local function CheckValue(Player)
    local Value = Player.Cash.Value -- Example.
    local NewValue = tonumber(Value.Value / Value.Value * math.random(math.floor(1, #Value.Value - Value.Value / 5))) -- Example: 1000 / 1000 * 1000 - 1000 / 5 = 800. Lower the number, the less it will take away.


CheckValue(game.Players.Megalodox_Official) -- Example person, actually use the player who died after testing.

This is untested, but should work. Let me know about any problems regarding this post! :smile:

1 Like

Seems like you want to take away a lower percentage of money from the user the more money they have. A possible solution is to make use of the math.log function. log increases slower the higher the value (because it is opposite of exponent). But, a simpler way is to just subtract a percentage from a range:

local cash = --> amount of cash the user has
local minP, maxP = 0.1, 0.9 --> takes 10% - 90% of their cash upon death
local minCash, maxCash = 0, 1000000000
    [minCash] specifies when [minP]% will be taken away while [maxCash]
    specifies when [maxP]% will be taken away. Increases linearly
local percentCashRemoved = minP + (maxP - minP) * math.clamp(
    (maxCash - cash) / maxCash,
) --> lerping values
local cashRemovedOnDeath = cash * percentCashRemoved

This method gives you control when the maximum and minimum percentage would be taken away, while also giving you control on how much.

1 Like

You should have tested this before sharing it.

First of all, math.floor

This function only has one argument. This leads us to

Since math.floor only recieves one argument, it also returns one value. math.random. When passed only 1 value, it returns a random value between 1 and the given value. In this case, you passed 1 as the value, so a random number between 1 and 1 is 1. No difference.

Let’s say it was an accident, and you meant to put 1 outside of math.floor, this is will error since the maximum can never be lower than minimum. What do I mean?, Value is a number and will error if you try to invoke any metamethods (e.g. #Value). What if it was a string? #Value returns a number, great. But, any integer will return a negative number. Look at this

#("10") = 2
2 - 10/5
2 - 2 = 0

#("4") = 1
1 - 4/2
1 - 2 = -1

Lower than 1!
Like I said a while ago, maximum can’t be lower than minimum. What if this is also an accident? What if you meant to put it the other way?

Well here is the part where it does not really matter what you do.

Value / Value

If your dividing a number by itself, it’s always equal to 1. So no matter what you do, the math.random is multiplying it by 1 which is equal to itself.

1 Like

The point is so they won’t lose too much. You can make 15 bucks in 1 click so I don’t think losing 9 will be a big problem, if you own 10 billions and you lost 20 millions it’s enough to warn you but again not much

Did you test this?

Ok, first of all, typos

Aside from that, look at this

That means, the higher the cash is, the greater cash it takes. Examples:

minP, maxP = 10, 90
maxCash = 1000
cash = Rand()

lerp = 10 + (90 - 10) * (500/1000)
lerp = 10 + (80) * 0.5
lerp = 10 + 40
lerp = 50

And also, if it is a percent, it means it should be a decimal.
minP and maxP should be decimals

local minP, maxP = 0.1, 0.9
1 Like

Seems like you just need a formula with a clamp, i.e;

	local leaderstats = player.leaderstats
	local money = leaderstats.Money
	money.Value -= math.clamp(money.Value / 10, math.min(money.Value, 1000), 1000000)

On death this would reduce a player’s money stat by 10%, clamping the subtracted value between a minimum of 1,000 (or however much money the player has) and a maximum of 1,000,000, these values can be changed to your desire.