Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[SOLVED]Is there any way to get every nth number? [closed]

Asked by
LawlR 182
6 years ago
Edited 6 years ago
1if CurrentRound == 10 or CurrentRound == 20 or CurrentRound == 30 or CurrentRound == 40 or CurrentRound == 50 then -- etc..
2    -- Do something
3end

I'm wondering if there's a cleaner way of doing this. Any help will be appreciated!

EDIT: I made it slightly cleaner.

1local SpecialRounds = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
2for i,v in pairs(SpecialRounds) do
3    if CurrentRound == v then
4        -- Do something
5    end
6end

Let me know if there's an even cleaner way of doing this. Thanks!

0
There is a better way. Check out my answer. User#21908 42 — 6y
1
Uh... Has anyone heard of a step? The third optional parameter of the neumeric for loop? for i=0, 1000, 10 do print(i) end M39a9am3R 3210 — 6y
0
:p User#19524 175 — 6y

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?

3 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

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:

01local specialRounds = {
02    10,
03    20,
04    30,
05    40,
06    50
07} -- and so on
08local 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
15end
16-- here I will put an example
17local round = 30
18if checkRound(round, specialRounds) then
19    --code
20end

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:

1local currentRound = 15
2if currentRound % 10 == 0 then
3    -- code
4end

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:

01local highestLevel = 100
02local 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
09end
10local currentRound = 20
11if checkRound(currentRound, highestLevel) then
12    -- code
13end

I hope this helps clarify what he meant. Have a great day scripting!

0
good job you really know your maths User#19524 175 — 6y
0
Thanks! :D User#21908 42 — 6y
Ad
Log in to vote
0
Answered by
systack 123
6 years ago
Edited 6 years ago

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.

1if CurrentRound % 10 == 0 then
2    --Code
3end
Log in to vote
0
Answered by 6 years ago

if you are using alot of rounds, you can use a numeric for loop instead of a generic for loop.

1local currentround = 57
2for i = 0,100 do
3    if currentround = i then
4        ...
5    end
6end

come to think of it, this is one of the few times numeric for loops are superior to generic for loops lol.