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

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

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!

3 comments

0

There is a better way. Check out my answer.

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

0

:p

3 answers

✓ Accepted answer

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!

2 comments

0

good job you really know your maths

0

Thanks! :D

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

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.

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