Conditional Statements
Conditional statements allow you to check to see if something is true or not. They are often used with relational and logical operators. You will be using if, else, and elseif.
if
An if statement tests a condition:
if workspace:FindFirstChild'Hint' == nil then -- if there isn't a hint in workspace
Instance.new("Hint", workspace).Text = "Hi!" -- make a hint
print 'Hint created!' -- print a string
end -- end the if statement
This uses the FindFirstChild method to locate a hint. Remember that you can use logical operators like 'and' and 'or' as well as variables if you need to.
x = true -- create a variable
if (workspace.Part ~= nil) and (x == true) then -- if the part is there, and x is true
workspace.Part.BrickColor = BrickColor.new("Bright red") -- change the color
end -- end the statement
else
See? But you might still be wondering what else and elseif are. They are optional, for when a condition doesn't meet the requirements of an if statement.
if (5 == 2) then -- do this if 5 = 2
print '5 = 2! H4X!'
else -- otherwise, do this
print 'Whew, 5 isn't equal to 2.'
end -- end
Easy enough, right? Elseif works similarily to if.
elseif
if workspace:findFirstChild'TheDarkFireDragon' ~= nil then -- if I'm here
print 'Dragon in da house!'
elseif workspace:findFirstChild'Telamon' ~= nil then -- If I'm not here, but Telamon is
print 'No Dragon, but look! Telamon!'
elseif workspace:findFirstChild'YourNameHere' ~= nil then -- No Telamon or I, just you
print 'Where did those 2 go?'
else
print 'Where are the cool people?! D:'
end
Congratulations, you now know how to use if, else, and elseif statements!
Further reading: ROBLOX Wiki, Programming in Lua
if
An if statement tests a condition:
if workspace:FindFirstChild'Hint' == nil then -- if there isn't a hint in workspace
Instance.new("Hint", workspace).Text = "Hi!" -- make a hint
print 'Hint created!' -- print a string
end -- end the if statement
This uses the FindFirstChild method to locate a hint. Remember that you can use logical operators like 'and' and 'or' as well as variables if you need to.
x = true -- create a variable
if (workspace.Part ~= nil) and (x == true) then -- if the part is there, and x is true
workspace.Part.BrickColor = BrickColor.new("Bright red") -- change the color
end -- end the statement
else
See? But you might still be wondering what else and elseif are. They are optional, for when a condition doesn't meet the requirements of an if statement.
if (5 == 2) then -- do this if 5 = 2
print '5 = 2! H4X!'
else -- otherwise, do this
print 'Whew, 5 isn't equal to 2.'
end -- end
Easy enough, right? Elseif works similarily to if.
elseif
if workspace:findFirstChild'TheDarkFireDragon' ~= nil then -- if I'm here
print 'Dragon in da house!'
elseif workspace:findFirstChild'Telamon' ~= nil then -- If I'm not here, but Telamon is
print 'No Dragon, but look! Telamon!'
elseif workspace:findFirstChild'YourNameHere' ~= nil then -- No Telamon or I, just you
print 'Where did those 2 go?'
else
print 'Where are the cool people?! D:'
end
Congratulations, you now know how to use if, else, and elseif statements!
Further reading: ROBLOX Wiki, Programming in Lua
More with Conditional Statements
You may see code that looks like this:
if something then
--dothis
end
What the heck does "if something then" mean?! I'll show you.
if something then is the equivalent of if something ~= nil then. But obviously, this is a shorter method than adding the "~= nil" to the end.
So how does it work?
First, think about what you're saying. If something is nil, it's not there. Remember that "~=" to is like saying "is not" Thus, "if something ~= nil then" is like saying:
"if something is not not there do this"
But see the 2 "nots"? They cancel each other out. Because they cancel each other out in English, they will in Lua too. This is how we get to "if something then." Note that in this example, though, something needs to be a variable for the code to run. However, the same rules apply for things like "if workspace:FindFirstChild'Player' then"
Okey dokey? You can write this type of statement however you want.
if something then
--dothis
end
What the heck does "if something then" mean?! I'll show you.
if something then is the equivalent of if something ~= nil then. But obviously, this is a shorter method than adding the "~= nil" to the end.
So how does it work?
First, think about what you're saying. If something is nil, it's not there. Remember that "~=" to is like saying "is not" Thus, "if something ~= nil then" is like saying:
"if something is not not there do this"
But see the 2 "nots"? They cancel each other out. Because they cancel each other out in English, they will in Lua too. This is how we get to "if something then." Note that in this example, though, something needs to be a variable for the code to run. However, the same rules apply for things like "if workspace:FindFirstChild'Player' then"
Okey dokey? You can write this type of statement however you want.