Functions
Functions in Lua perform a certain task. They can be used similarly as variables to prevent repeating large chunks of code, or to trigger code when something happens. Some functions, like print, are predefined, but you can create your own too!
We'll start by creating a function.
function MyFirstFunction()
This is how a function is created. You say that it's a function, then give it a name that ends in parentheses. Next, give your function a chunk of code. Remember to end the function with an 'end'.
function HiDer() -- create a function
if workspace:findFirstChild'Telamon' then -- give it some code
print 'Hi Telamon!' -- if Telamon's there, say hello
else
print "Aww, he's not here!"
end -- end the conditional statement we're using in this example
end -- end the function
Now we have our first function! But it won't do anything yet. To call a function, simply state the function's name:
HiDer() -- This will print something, if you're using the function above
Further reading: ROBLOX Wiki, Programming in Lua
We'll start by creating a function.
function MyFirstFunction()
This is how a function is created. You say that it's a function, then give it a name that ends in parentheses. Next, give your function a chunk of code. Remember to end the function with an 'end'.
function HiDer() -- create a function
if workspace:findFirstChild'Telamon' then -- give it some code
print 'Hi Telamon!' -- if Telamon's there, say hello
else
print "Aww, he's not here!"
end -- end the conditional statement we're using in this example
end -- end the function
Now we have our first function! But it won't do anything yet. To call a function, simply state the function's name:
HiDer() -- This will print something, if you're using the function above
Further reading: ROBLOX Wiki, Programming in Lua
Arguments
Inside of a function's parentheses, you can put arguments. You can use these arguments later in the function; they work similarly to variables. Here is a nice example from the wiki:
function Add(num1, num2) -- function with 2 arguments
print(num1 + num2) -- add the arguments
end -- end the function
Add(5, 7) -- Call the function. Give the arguments (in this case) numbers
This will output 12, because the function added the arguments (see line 2). We give the arguments values, 5 (num1) and 7 (num2). Understand?
Something often used in functions that you should know about is return. It can be used in place of print, but code below it won't run. Here's another example from the wiki:
function Add(num1, num2)
print("Finding sum")
return num1 + num2
print("Sum found")
end
Add(2, 4)
This will output:
Finding sum
6
It won't output "Sum found" because that is after the return statement.
function Add(num1, num2) -- function with 2 arguments
print(num1 + num2) -- add the arguments
end -- end the function
Add(5, 7) -- Call the function. Give the arguments (in this case) numbers
This will output 12, because the function added the arguments (see line 2). We give the arguments values, 5 (num1) and 7 (num2). Understand?
Something often used in functions that you should know about is return. It can be used in place of print, but code below it won't run. Here's another example from the wiki:
function Add(num1, num2)
print("Finding sum")
return num1 + num2
print("Sum found")
end
Add(2, 4)
This will output:
Finding sum
6
It won't output "Sum found" because that is after the return statement.
Events
Now it's time to make your functions even more cool! Events allow functions to automatically be called when something happens. So, instead of calling a function the regular, boring way, we can use an event connection line. This way, functions can be activated when a player says something, touches a brick, joins a game, or any other number of things. (Be sure to check out the totally awesome complete list here!)
We'll start by making a function that makes something happen when something touches a brick. We'll make it print a message to the output when this happens. This type of function is commonly named 'onTouch', (because the event fires on touch) but you can name it anything.
function onTouch()
print 'Hello world!'
end
Great! But we need to let ROBLOX know that we want this function to fire when someone touches our brick. To do this, we'll need a connection line. Connection lines are used in the format shown below:
(object, often script.Parent).(Event name):connect((function name))
Of course, you'll have to modify the text that I highlighted. Here is what the function above, with its connection line, would look like:
function onTouch() -- function
print 'Hello world!' -- print
end -- end function
script.Parent.Touched:connect(onTouch) -- connection line
(Of course, the highlighting here won't show up on ROBLOX)
Here is that function again, but this time we'll pass an (optional) argument.
function onTouch(hit)
print(hit.Parent) -- print the name of what hit the brick
end
script.Parent.Touched:connect(onTouch)
Here is another example, this time we'll use the PlayerAdded so our function fires when someone joins the game.
function PlayerJoined(p)
print(p.Name) -- print the player who joins
end -- (This example is from the ROBLOX wiki, by the way.)
game.Players.PlayerAdded:connect(PlayerJoined)
We'll start by making a function that makes something happen when something touches a brick. We'll make it print a message to the output when this happens. This type of function is commonly named 'onTouch', (because the event fires on touch) but you can name it anything.
function onTouch()
print 'Hello world!'
end
Great! But we need to let ROBLOX know that we want this function to fire when someone touches our brick. To do this, we'll need a connection line. Connection lines are used in the format shown below:
(object, often script.Parent).(Event name):connect((function name))
Of course, you'll have to modify the text that I highlighted. Here is what the function above, with its connection line, would look like:
function onTouch() -- function
print 'Hello world!' -- print
end -- end function
script.Parent.Touched:connect(onTouch) -- connection line
(Of course, the highlighting here won't show up on ROBLOX)
Here is that function again, but this time we'll pass an (optional) argument.
function onTouch(hit)
print(hit.Parent) -- print the name of what hit the brick
end
script.Parent.Touched:connect(onTouch)
Here is another example, this time we'll use the PlayerAdded so our function fires when someone joins the game.
function PlayerJoined(p)
print(p.Name) -- print the player who joins
end -- (This example is from the ROBLOX wiki, by the way.)
game.Players.PlayerAdded:connect(PlayerJoined)
Anonymous Functions
Anonymous functions are functions without names. They are often used with events, or are stored in variables because they don't have names. Here's the wiki's example:
hi = function() print 'hi' end
is the same as
function hi() print 'hi' end
Anonymous functions are commonly used with events. Here is the second event example from above, but this time written as an anonymous function.
script.Parent.Touched:connect(function(hit) -- function, no need to name it!
print(hit.Parent)
end) -- end the function. Remember to close the parentheses!
Finally, in this example from the wiki, we'll put a function inside of another function. We're using a the Chatted event which fires when a specific player chats. Because of the PlayerAdded function's argument, this 'reset script' will work with every player.
game.Players.PlayerAdded:connect(function(player) --PlayerAdded function
player.Chatted:connect(function(msg) -- Chat function
if msg == 'reset' then --if the message is this,
player.Character:BreakJoints() -- reset the player.
end -- end the if statement
end) -- end the chat function
end) -- end the first function
hi = function() print 'hi' end
is the same as
function hi() print 'hi' end
Anonymous functions are commonly used with events. Here is the second event example from above, but this time written as an anonymous function.
script.Parent.Touched:connect(function(hit) -- function, no need to name it!
print(hit.Parent)
end) -- end the function. Remember to close the parentheses!
Finally, in this example from the wiki, we'll put a function inside of another function. We're using a the Chatted event which fires when a specific player chats. Because of the PlayerAdded function's argument, this 'reset script' will work with every player.
game.Players.PlayerAdded:connect(function(player) --PlayerAdded function
player.Chatted:connect(function(msg) -- Chat function
if msg == 'reset' then --if the message is this,
player.Character:BreakJoints() -- reset the player.
end -- end the if statement
end) -- end the chat function
end) -- end the first function