Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a function that fires an event once a number is achieved? (More info in desc)

I really want to make a function that for example, Every five levels the player gets +2 damage or something? in code: (idrk what to put but):

if player.Data.Level == (original level) + (5 more levels than original) then
player.Data.Damage = player.Data.Damage + 2
end

and I want this to keep going every time the player gets 5 more levels than what he previously had.

5 comments

0

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.

0

but if I did that, the script would be adding damage even if the player hasn't achieved another 5 levels

0

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

0

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

0

Sure thing

1 answer

✓ Accepted answer

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 --the amount of levels we need before our damage increases
groups = level / per_level
extra_damage = groups * damage --> 9 Groups x 7 damage = 63 extra 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 --> we have a remainder of 1 because 6 is over by 5 by 1. 5 cannot fit into 6
--if we subtract the remainder from our level, it is 5. This is true if I have 7, 8 and 9 as well.


level = 10
remainder = level % 5 --> there is no remainder, because 5 can fit into 10.
--if we subtract 0 we get 10, which is still in every 5 levels!

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 --the amount of levels we need before our damage increases
remainder = level % per_level --the remainder from our level and per_level
groups = (level - remainder) / per_level --how many groups we have from this scaled down number (for every 5...)
extra_damage = groups * damage --our unit which is scaled down and multiplied by the damage we want

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 --this loop will run 3 times because per_level (5) fits into level(17) 3 times! same logic
	totalDamage = totalDamage + 2 --add our current damage by 2 each per_level
end
print( totalDamage ) --> 6

I hope this helps, and I remind you to go and learn modulus more as it is used everywhere.

1 comment

0

yep this basically answers my question. Thanks bruv

This discussion is preserved as it was. New answers, comments, and votes are closed.