Boolean: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>"
 
(46 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Map|[[Scripting]]|[[Data Types]]}}
{{Map|Scripting|Data Types}}


{{CatUp|Data Types}}
A '''Boolean''', or '''Bool''' value, is a very simple data type. It is either a '''true''' or '''false''' value.
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 treated as '''true''' when converted to a boolean, unless it is '''nil''' or '''false'''.
In Lua, when something is converted to a boolean, if the value is false or nil then it will be false, otherwise it is true.


<!--a bit too repetitive
{{EmphasisBox|
{{EmphasisBox|
One easy way to think of Boolean values is like a light switch. A switch has two positions, on and off. Only a Boolean value only includes two values, true and false.
One easy way to think of Boolean values is like a light switch. A switch has two positions, on and off. Only a Boolean value only includes two values, true and false.
}}
}}
 
-->
Booleans are really easy to use. Like so:
== Using Booleans ==
<code lua>
Booleans are most commonly used with [[Conditional_statements|conditional statements]].
<syntaxhighlight lang="lua">
MyBool = true
MyBool = true


Line 19: Line 20:
     --If "MyBool"'s value is false, this code is run.
     --If "MyBool"'s value is false, this code is run.
end
end
</code>
</syntaxhighlight>


==Converting booleans into strings==
==Converting booleans into strings==
If you want to turn a boolean into "true" or "false", you can do so with tostring(). If you want to make them display something else, you have to do it yourself
When converted to a string, booleans will return {{`|"true"}} or {{`|"false"}}
{{code and output|code=
print(tostring(true))
print(tostring(false))
|output=
true
false
}}


A typical example would be this:
If you wanted it to print something other than true or false, you will have to use a [[Conditional_statements|conditional statements]]


<code lua>
<syntaxhighlight lang="lua">
local enabled = false
local enabled = false


Line 34: Line 42:
     print("Disabled")
     print("Disabled")
end
end
</code>
</syntaxhighlight>


However, there is a cleaner way of doing this. You can use the following idiom:
However, there is a cleaner way of doing this. You can use the following idiom:


print(enabled and "Enabled" or "Disabled")
<syntaxhighlight lang="lua">print(enabled and "Enabled" or "Disabled")</syntaxhighlight>
 
and get the same results.


==Operators==
==Operators==


===Not===
===Not===
The '''not''' operator returns true if the argument is false or {{nil}}, otherwise it will return false. The table below shows some example results
{| class="wikitable" width="100%"
! scope="row"| {{`|x}}
| true  || false || nil  || "text" || 0    || 1
|-
! scope="row"| {{`|not x}}
| false || true  || true || false  || false || false
|}


The '''not''' operator returns true if the argument is false or [[nil]], and false otherwise.


<code lua>
{{Example|
print(not false)      -- true
One thing that the not operator is useful for is toggling something. To make a button toggle the visibility of a GUI, you can do:
print(not nil)        -- true
 
print(not true)      -- false
<syntaxhighlight lang="lua">
print(not "A string") -- false
button.MouseButton1Click:connect(function()
print(not 0)         -- false
    frame.Visible = not frame.Visible
print(not 1)         -- false
end)
</code>
</syntaxhighlight>
}}


===And===
===And===
The '''and''' operator returns the first argument if it is false or [[nil]], otherwise it will return the second argument.


The '''and''' operator returns the first argument if it is false or [[nil]], and the second argument otherwise.
<syntaxhighlight lang="lua">
 
<code lua>
print(4 and 5)        --> 5
print(4 and 5)        --> 5
print(nil and 13)      --> nil
print(nil and 13)      --> nil
Line 68: Line 86:
print(false and true) -- false
print(false and true) -- false
print(false and false) -- false
print(false and false) -- false
</code>
</syntaxhighlight>
 
{{code and output
|code=
print(true and nil)
print(false and 5)
print({} and "hello")
|output=
nil
false
hello
}}


===Or===
===Or===
{{main|or operator}}
The '''or''' operator returns the first argument if it is neither false nor [[nil]], otherwise it will return the second argument.


The '''or''' operator returns the first argument if it is neither false nor [[nil]], and the second argument otherwise.
<syntaxhighlight lang="lua">
 
<code lua>
print(true or true) -- true
print(true or true) -- true
print(true or false) -- true
print(true or false) -- true
Line 82: Line 111:
print(4 or 5)          --> 4
print(4 or 5)          --> 4
print(false or 5)      --> 5
print(false or 5)      --> 5
</code>
</syntaxhighlight>


== See Also ==
== See also ==


* [[RBX.lua.BoolValue (Object)]]
* [[RBX.lua.BoolValue (Object)]]
Line 91: Line 120:


* [http://lua-users.org/wiki/LuaTypesTutorial Lua Types Tutorial]
* [http://lua-users.org/wiki/LuaTypesTutorial Lua Types Tutorial]
* [[Not operator]]
* [[And operator]]


* [[Or operator]]
* [[Or operator]]


[[Category:Data Types]]
[[Category:Data types]]

Latest revision as of 06:17, 27 April 2023

A Boolean, or Bool value, is a very simple data type. It is either a true or false value.

In Lua, when something is converted to a boolean, if the value is false or nil then it will be false, otherwise it is true.

Using Booleans

Booleans are most commonly used with conditional statements.

MyBool = true

if MyBool then
    --If "MyBool"'s value is true, this code is run.
else
    --If "MyBool"'s value is false, this code is run.
end

Converting booleans into strings

When converted to a string, booleans will return "true" or "false"

print(tostring(true))
print(tostring(false))

true

false

If you wanted it to print something other than true or false, you will have to use a conditional statements

local enabled = false

if enabled then
    print("Enabled")
else
    print("Disabled")
end

However, there is a cleaner way of doing this. You can use the following idiom:

print(enabled and "Enabled" or "Disabled")

and get the same results.

Operators

Not

The not operator returns true if the argument is false or nil, otherwise it will return false. The table below shows some example results

x true false nil "text" 0 1
not x false true true false false false


Example

One thing that the not operator is useful for is toggling something. To make a button toggle the visibility of a GUI, you can do:

button.MouseButton1Click:connect(function()
    frame.Visible = not frame.Visible
end)


And

The and operator returns the first argument if it is false or nil, otherwise it will return the second argument.

print(4 and 5)         --> 5
print(nil and 13)      --> nil
print(false and 13)    --> false

print(true and true) -- true
print(true and false) -- false
print(false and true) -- false
print(false and false) -- false
print(true and nil)
print(false and 5)
print({} and "hello")

nil false

hello

Or

Main article: or operator

The or operator returns the first argument if it is neither false nor nil, otherwise it will return the second argument.

print(true or true) -- true
print(true or false) -- true
print(false or true) -- true
print(false or false) -- false

print(4 or 5)          --> 4
print(false or 5)      --> 5

See also