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
if CurrentRound == 10 or CurrentRound == 20 or CurrentRound == 30 or CurrentRound == 40 or CurrentRound == 50 then -- etc..
    -- Do something
end

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

EDIT: I made it slightly cleaner.

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

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:

local specialRounds = {
    10,
    20,
    30,
    40,
    50
} -- and so on
local function checkRound(currentRound, rounds)
    for i,v in pairs(rounds) do
        if currentRound == v then
            return true
        end
    end
    return false -- because return breaks out of a function, if the return true is never reached, this function will return false
end
-- here I will put an example
local round = 30
if checkRound(round, specialRounds) then
    --code
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:

local currentRound = 15
if currentRound % 10 == 0 then
    -- code
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:

local highestLevel = 100
local function checkRound(currentRound, maxLevel)
    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
        if currentRound == i then
            return true -- is a multiple of ten because we are growing by ten
        end
    end
    return false -- same concept as in the function above
end
local currentRound = 20
if checkRound(currentRound, highestLevel) then
    -- code
end

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.

if CurrentRound % 10 == 0 then
    --Code
end
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.

local currentround = 57
for i = 0,100 do
    if currentround = i then
        ...
    end
end

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