For variable assigment, "or" or "if then else" should be used?

I have recently found today that weirdly, to assign variables you could use if then else…
image
which weirdly actually works


I found this sometimes to be kinda like making a variable where you sometimes use “or”

local value1 = nil
local value2 = "stuff"

local epicStuffNameNoIDea = value1 or value2
--(basically = if value1 then value1 else value2)

So i was wondering which is better? or or if then else?
from that style lua guide stuff idk i forgot, apparently you should always use if then else instead of or but i personally think it comes from situation (like you wouldn’t want to repeat value1 when you can just use or) or personal preference

If your value is as simple as you showed us then (“or/and”) it would be great. But, for complicated values I personally do use

local value = value1 == 2 and value2 == 1 and value1 or value2

but you should use if-then-else as it’s easier to understand.

1 Like

It’s better to use if-then-else, because it’s mentioned in the Roblox Lua Style Guide

1 Like

i didn’t even know you could do that with and

Well know you do. I usually overuse that method lol.

It’s also a custom luau feature that doesnt exist in any other version of lua. A lot more readable too.

1 Like