1 | if CurrentRound = = 10 or CurrentRound = = 20 or CurrentRound = = 30 or CurrentRound = = 40 or CurrentRound = = 50 then -- etc.. |
2 | -- Do something |
3 | end |
I'm wondering if there's a cleaner way of doing this. Any help will be appreciated!
EDIT: I made it slightly cleaner.
1 | local SpecialRounds = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 } |
2 | for i,v in pairs (SpecialRounds) do |
3 | if CurrentRound = = v then |
4 | -- Do something |
5 | end |
6 | end |
Let me know if there's an even cleaner way of doing this. Thanks!
Generally with this kind of problem I suggest an array. Then use a function to loop through the array and see if the item is in that array. Here is an example:
01 | local specialRounds = { |
02 | 10 , |
03 | 20 , |
04 | 30 , |
05 | 40 , |
06 | 50 |
07 | } -- and so on |
08 | local function checkRound(currentRound, rounds) |
09 | for i,v in pairs (rounds) do |
10 | if currentRound = = v then |
11 | return true |
12 | end |
13 | end |
14 | return false -- because return breaks out of a function, if the return true is never reached, this function will return false |
15 | end |
16 | -- here I will put an example |
17 | local round = 30 |
18 | if checkRound(round, specialRounds) then |
19 | --code |
20 | end |
If you are wanting to only check if the round is a multiple of ten than you can simply use the modulus %
operator. It will divide the number by whatever number you choose and return the remainder. This means that if you have a remainder of 0 the number is divisible by ten. Here is an example:
1 | local currentRound = 15 |
2 | if currentRound % 10 = = 0 then |
3 | -- code |
4 | end |
I hope this helps and have a great day scripting! Edit: What theking48989987 was going for was incrementing by ten. I just wanted to clarify so that people understood his method a little better. I will also use a function so that it can be used as many times as you might want. Example of method:
01 | local highestLevel = 100 |
02 | local function checkRound(currentRound, maxLevel) |
03 | for i = 10 , maxLevel, 10 do -- growing by ten every loop and I am starting at ten because that is the first value you check in your example |
04 | if currentRound = = i then |
05 | return true -- is a multiple of ten because we are growing by ten |
06 | end |
07 | end |
08 | return false -- same concept as in the function above |
09 | end |
10 | local currentRound = 20 |
11 | if checkRound(currentRound, highestLevel) then |
12 | -- code |
13 | end |
I hope this helps clarify what he meant. Have a great day scripting!
It seems like each 'special round' is one which is a multiple of ten (or every tenth round), so you can just check whether CurrentRound % 10 == 0
evaluates to true.
i.e.
1 | if CurrentRound % 10 = = 0 then |
2 | --Code |
3 | end |
if you are using alot of rounds, you can use a numeric for loop instead of a generic for loop.
1 | local currentround = 57 |
2 | for i = 0 , 100 do |
3 | if currentround = i then |
4 | ... |
5 | end |
6 | end |
come to think of it, this is one of the few times numeric for loops are superior to generic for loops lol.
Locked by User#19524
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?