Okay, this’ll be a quick one, ternary like operation in Lua.
You think of a ternary operation as a shorthand if-else statement, which is used to take up less space in your codebase.
Consider this code that tells you if a result is greater than 4. Somehow the best example I could come up with.
local result = 0
if result > 4 then
message = "I am greater than 4!"
else
message = "I am less than or equal to 4!"
end
print(message)
However this check can be shortened to one line.
local result = 0
local message = result > 4 and "I am greater than 4!" or "I am less than or equal to 4!"
print(message) --> "I am less than or equal to 4!"
All this is, is some syntactic sugar, so you don’t need to feel obligated to do this.
A more practical example would be this code here, in my “place bomb” effect.
KillerQueen.Bomb.OnClientEvent:Connect(function(stand, placeBomb)
if placeBomb then
move(stand, CFrame.new(0,0, -3), info):Play()
else
move(stand, summonCFrame, info):Play()
end
end)
Which yields this result.
However that code can be shortened to one line.
KillerQueen.Bomb.OnClientEvent:Connect(function(stand, placeBomb)
move(stand, placeBomb and CFrame.new(0,0, -3) or summonCFrame, info):Play()
end)
It yields the same result, just in a smaller package.
So let me review what you’ve learned.
You can do ternary like operation in Lua by using the logical or, and statements.
You start off with the truthy condition, then you can use the logical or to run a default condition. AKA the “else” part of the code.
You can do if, elseif, and else with ternary operation like this.
local result = 3
local message = result > 4 and "I am greater than 4!" or result == 3 and "I am equal to 3!" or "I am less than 4!"
print(message) --> "I am equal to 3!"
However this can be messy to other scripters, so it’s your choice if you’d like to do it.
I recommend simple if-else conditions, as those are simple and easy to understand but you can do whatever you want.