The best way to solve this is with maths (division and modulus)
First, let's divide out our levels by 5, or in other words, how many groups of 5 levels do we have? If the answer is 5, we have 1 group - this will be our Unit. (I know this is obvious, bare with me).
For this example, I want to be producing 7 damage. We know that our 1 group of 5 levels should be 7 damage, so lets multiply our group by 7. This goes the same with 2 groups of 5 levels (2x7 = 14 extra damage), or 3 groups (3x7 = 21 damage). So let it be known that Groups x Damage = Total extra damage. In code, it can look like this
level = 45
damage = 7
per_level = 5
groups = level / per_level
extra_damage = groups * damage
Problem: If we have a remainder from our damage per level (5), it will calculate the remainder into our extra_damage variable, making our system incorrect. If we ONLY want 7 damage per 5 levels, then we need to remove the remainder. To find the remainder, you use Modulus. Modulus is simply a math operation which calculates a remainder between two numbers. In our case, our first operand will be the our level and the 2nd operand being 5.
Here is an example.
level = 6
remainder = level % 5
level = 10
remainder = level % 5
Okay, now to apply the two sections. Get the unit by removing the remainder and then dividing, then multiply that unit by the damage to find x!
level = 73
damage = 7
per_level = 5
remainder = level % per_level
groups = (level - remainder) / per_level
extra_damage = groups * damage
Less abstractly, like what I commented and related to your code, you can do this - which is the exact same thing
damage = 2
per_level = 5
player.Data.Damage = (player.Data.Level - player.Data.Level%per_level) / per_level * damage
You can think of a remainder as starting at 0 and increases by 1 each time, until the number reaches one less than the number we are dividing by. After that, the sequence repeats. I highly recommend learning it, it's fundamental and comes in a great number of uses. I'm sorry that I wasn't able to fully explain modulus, it's probably for the best.
If you still don't understand, there's another solution you can do that produces the same result, although less efficient.
Have a for loop for that increments every 5 levels until it hits your level, in its scope, just keep adding the up the damage to a variable, like so:
level = 17
per_level = 5
totalDamage = 0
for i=0, level, per_level do
totalDamage = totalDamage + 2
end
print( totalDamage )
I hope this helps, and I remind you to go and learn modulus more as it is used everywhere.
5 comments
Make a formula for the extra damage. Divide the player's level by 5 to get your unit, then multiply the unit by x. So something like player.Data.Damage = (player.Data.Level / 5) * 2 for 2 points of extra damage per 5 levels.
but if I did that, the script would be adding damage even if the player hasn't achieved another 5 levels
Then you use modulus. If we're level 62, 62 mod 5 == 2, then subtract that remainder from the equation. It should look something like this: ((player.Data.Level - player.Data.Level%5) / 5) * 2
I don't really get it but could you explain the code in the answer section? So that I could accept your code if it works
Sure thing