Set a Boolean Variable in Just One Line of Code?

Im just wondering if there is an easy way to set a variable like this?

local boolean = (1 = 1) 
print(boolean) -- True

local boolean = (1 = 2)
print(boolean) -- False

I’m trying to avoid using an entire If Else statement to set a single variable like this:

if statement == true then
    boolean = true
else
    boolean = false
end

If there’s not an easy work around then thats alright, Im just trying to find possible solutions.

1 Like
local boolean = number == 1 and true or false

“number == 1” is the logic part

“and” means that the logic endend

“true” is what the variable will be if the statement is true

“false” is what the variable will be if the statement is false

1 Like

Oh that makes alot of sense. I think I was doing number = 1 instead of number == 1. Oops! Thanks for the help though.

Just to add, you could do this for if you wanted elseif’s in that format.

local boolean = number == 1 and true or number == 2 and false

which is the equlivlent to:

if number == 1 then
    boolean = true
elseif number == 2 then
     boolean = false
end

and add this then if the number is none of those it would be true

or true 
2 Likes

This works a bit better for the code im trying to make. Thanks alot!

thanks, i didnt know about this

No worries!

Why not do this? I find it more readable and less redundant.
local boolean = number == 1

1 Like

i mean yes, this is for boolean. but i wanted to show a way to make the variable have different values instead of a boolean

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.