Hi, so I've recently been watching roblox studio videos on youtube and saw a line of code similar to this on it
i % 2 == 0 and "Blink" or "Off"
for i = 1,20 do
wait()
local value = i % 2 == 0 and "Blink" or "Off"
-- Rest of the code --
end
Hi, so I've recently been watching roblox studio videos on youtube and saw a line of code similar to this on it
i % 2 == 0 and "Blink" or "Off"
for i = 1,20 do
wait()
local value = i % 2 == 0 and "Blink" or "Off"
-- Rest of the code --
end
I'm assuming you already know how the modulo operator (%) works. In this case, i % 2 == 0 checks if i is even. The code you sent works because Lua uses short circuit evaluation which will be simplified to Lua being short circuited below.
Short circuiting works in Lua by only evaluating the second value if the first value doesn't determine the answer. For example x and y will equal y if x is truthy (a value which is not nil/false) and will equal false if x is falsy (nil/false) because, since x is false, it know that the expression should be falsy as y cannot change the result of the expression if x is falsy. This is also done by or. For example, in x or y, if x is truthy, it equals x and otherwise it equals y
In the code i % 2 == 0 and "Blink" or "Off" will be simplified to x and y or z. The expression mentioned recreates the ternary operator which means that if x is truthy, it equals y, and if not, it equals z. Because of how it exploits Lua's short circuiting, it means that y must be truthy or it will always equal z.
Hope this helps!
local value = i % 2 == 0 and "Blink" or "Off"
it means
value = condition and option if condition is true or option of condition is false
it's the same as writing:
if i % 2 == 0 then
value = "Blink"
else
value = "Off"
end
i%2==0 is checking if the number i is even/odd using the modulus operator.
if it is even (i%2 is 0) then set the value to blink, otherwise if it is odd, set the value to off.
The reason the line value = i % 2 == 0 and "Blink" or "Off" works is because of how lua handles truthy values. if i%2==0 is true
there are two things going on here
x = <condition> and <option1> or <option2> is like a shortened if statement
if condition is truthy (aka not nil and not false), then x will be set to option1, otherwise x will be set to option2
another way you could write x = <condition> and <option1> or <option2> would be like this:
if condition then
x = option1
else
x = option2
end
so essentially, your code is doing the same thing as this
if i % 2 == 0 then
value = "Blink"
else
value = "Off"
end
next, the i % 2 == 0 part
the % operator, or modulus operator, is essentially the same thing as the remainder when you divide numbers
in this case, i % 2 == 0 is used because the remainder of something being divided by 2 will either be 1 or 0
by counting up you get an alternating pattern of 1 and 0, which is useful since the light will turn off every other iteration
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
% can check if a number is multiple of another, and, if it is, it returns 0. If not, it returns how much is left to reach its multiple. For example, 12 is multiple of four, but two isnt, so 12%4=0 and 2%4=2.
Now those Blink and off are strings, either you copied the code wrong or the youtuber wrote it wrong, because they dont make sense at all. Maybe these Blink and Off you copied are actually variables that the creator made and he was checking if their values arent nill.