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 % for in Lua Scripting?

What is the % for in this script? It's the "if i % 2 == 0 then" part that confuses me.

for i = 1, 20 do

if i % 2 == 0 then 
print(i .. " even") 
else 
print(i .. " odd") 
end 
end

1 answer

✓ Accepted answer

It is the modulus operator.

It gets the remainder if you divide the first number by the second.

Examples:

print(5 % 2) --> 1 (Because 5 / 2 = 2, remainder 1)
print(10 % 4) --> 2 (10 / 4 = 2, remainder 2)

2 comments

0

Thanks.

1

Works on non-integers too: 5.1 % 0.2 is 0.1

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