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

What is the term for "Even" and "Odd" in Roblox Studio?

I would find it easier for me cause I was making this Repeat function so that x which is 1,gets +1 after the Repeat function.

When the Value of x turns to an even number,The Repeat function will stop.

1 comment

0

i normally use something like "if X % 2 == 0 then" for even or "if X % 2 ~= 0 then" for odd, if you're using ints.

2 answers

✓ Accepted answer

u can use the % sign to check if stuff is even or odd: like this

local number = 1
print(number % 2 == 0)

so, to put that in a repeat(i think) is to uhh:

local x = 1
repeat until x % 2 == 0

the % sign tells what the remainder of the division of 2 numbers is; so we check if the (number / 2)'s remainder is 0. if it is, then it is a even number. Else it is odd

29 comments

0

What does the sign % do?

0

division

0

you're just checking if X is divisible by Y

0

wouldn't it be / ?

0

i think a x.Changed is more efficient here

0

wait

0

how would it work?

0

i dont understand the question rochel. how would what work.

0

im actually not sure the difference between % and /, hopefully someone can shed light on that.

0

ok look i editted it

0

u right banana. https://developer.roblox.com/articles/Math-Operators heres a link for further explaining on the difference.

0

xd

0

% is "modulus" which is a word that makes my brain hurt.

0

Basically,I'm making a "KeyPressed" function using UserInputService.When the player presses the key,it would make x +1 so when x == 1,it will constantly print something out with a wait(0.2).If the player presses the key again.It will make x +1 which equals to 2.When it reaches an even number,the repeat function will stop

0

if x % 2 ~= 0 then printstuff end

0

uhh what

0

cant u make a toggle

0

i can do the whole script but thats pretty much the jist of it. if its not even then do the thing.

0

when I tried x % 2 == 0,it printed false.So what does that mean?

0

also ya u can just do true/false as to whether or not the function is running. seems simpler?

0

it means x was not an even integer

0

yeah,i think so.

0

Oh.So false means odd,true means even.

0

uhh yes

0

yes, and if you did ~= instead of == it would be the opposite.

0

but again, a much simpler way like lua mentioned is to just toggle a bool back and forth.

0

thanks for explaining this!

0

lol

0

10 / 3 = 3.3333..., 10 // 3 = 3, 10 % 3 = 1. / is normal division. // gets you only the whole part of a division. % gets you only the remainder of the division.

The proper way to check if a number is an odd number or an even number is to use the modulo operator.

The modulo operator is simply a percentage sign (%).

To further understand what the modulo operator does, simply look at the code below:

function modulo(a, b)
    return a - (math.floor(a/b)*b)
end

The way to use the modulo operator to determine if a number is odd or even is like this:

function checkNumber(num)
	if type(num) ~= "number" then num = tonumber(num) end
	return (num % 2 == 0 and "even" or "odd")
end

print(checkNumber(5))

Hope this helps! Let me know if you need any more explanation.

3 comments

0

I'm confused

0

Fixed some issues with formatting. Basically, the modulo operator just returns the remainder of a / b. It's a bit more advanced, so there could be some difficulties attempting to understand it.

0

I think your answer should've been accepted. It provides a clearer understanding of what it is instead of just giving random code.

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