Hello, recently I started using ternary operations to assign variables and optimize my code. It really helps to cut down on typing space. Then I wondered if it was possible to assign multiple variables using this in one line of code (using one local)
Something like this: local a, b = 3, 'hi' This is without using ternary operations.
I tried to do this based on the assignment of 2 variables from the previous example:
local ResultText, ResultColor = Condition and 'Success!', Color3.fromRGB(0, 255, 0) or 'Error!', Color3.fromRGB(255, 0, 0)
But for some reason I get an error:
Assigning 3 values to 2 variables leaves some values unused
How do I do this correctly, if at all possible? I would be grateful for your help.
You need to separate them, as it only works for one assignment at a time.
For example:
local a, b = conditionA and "A is true!" or "A is false!", conditionB and "B is true!" or "B is false!"
You might also want to consider if-then-else expression, as the lua idiom (roughly simulates ternary operators) might return unexpected results if b in something like a and b or c evaluates to false.
Thanks, that worked! I assign 2 variables as one, separating them incorrectly. Yes, I was thinking of using if-then-else but I thought I could do without it to save space.