Bool: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
>Mr Doom Bringer
No edit summary
Line 1: Line 1:
{{CatUp|Properties}}
{{CatUp|Value Types}}


== Introduction ==
== Introduction ==
A Bool, or Boolean Value is a <b>true</b> or <b>false</b> value. In Lua, either the value is <b>true</b>, or it is <b>false</b>/<b>nil</b>. When trying to change this value in code, use a <b>true</b> or a <b>false</b> variable.
A '''Boolean''', or '''Bool''' value is a very simple type of data. It can be either '''true''' or '''false'''. That's it. Either yes or no.  


These variables show up as a checkbox in the Properties window. <b>True</b> is a checked box, <b>false</b> is an unchecked box.
In Lua, everything that has a value is '''true''', unless it is '''nil''' or '''false'''.


== Examples ==
<pre>
x = true
print(x)
Will result in:
true


x = true
print(not x)
Will result in:
false


print(not false)
Booleans are really easy to use. Like so:
Will result in:
{{CodeExample}}
true
MyBool = true
</pre>


Boolean values are used to represent the results of logic tests. The equals ==, and not equals ~= operators will return boolean values depending on the values supplied to them.  
if MyBool then
<pre>
:--If "MyBool" is true, this code is run.
print(1 == 0) -- test whether two numbers are equal
else
Will result in:
:--If "MyBool" is false, this code is run.
false
end
|}


print(1 == 1)
Will result in:
true


print(1 ~= 0) -- test whether two numbers are not equal
Bools can also be used as numbers. '''true''' is 1 and '''false''' is 0. Sometimes you will see scripts like this:
Will result in:
{{CodeExample}}
true
MyBool = 1


print(true ~= false) -- is true not equal to false?
if MyBool == true then
Will result in:
:--This code is run, since MyBool is '''1''', and thus true.
true
else
</pre>
:--This bit is not run, since MyBool is true, not false
end
|}





Revision as of 21:22, 9 March 2010

Introduction

A Boolean, or Bool value is a very simple type of data. It can be either true or false. That's it. Either yes or no.

In Lua, everything that has a value is true, unless it is nil or false.


Booleans are really easy to use. Like so: Template:CodeExample MyBool = true

if MyBool then

--If "MyBool" is true, this code is run.

else

--If "MyBool" is false, this code is run.

end |}


Bools can also be used as numbers. true is 1 and false is 0. Sometimes you will see scripts like this: Template:CodeExample MyBool = 1

if MyBool == true then

--This code is run, since MyBool is 1, and thus true.

else

--This bit is not run, since MyBool is true, not false

end |}


See Also

RBX.lua.BoolValue (Object)

Programming in Lua 2.2: Booleans

Lua Types Tutorial