You're incorrectly conflating events and functions.
Events, such as Touched, are connected to functions via the connect method (which is actually also a function!)
Functions are, very simply, "blocked off" sections of code.
The parentheses have two meanings, depending on if you're defining or calling the function:
function FuncName(param1, param2)
print(param1)
print(param2)
end
FuncName("hooplah", 12)
param1 and param2 are the parameters of the function. They are filled by passed-in arguments "hooplah" and 12 on line 8. You can have a very large (on the order of thousands) of parameters before Lua will start yelling at you, but having more than a handful is a good sign you're doing too much in a single function.
You can write functions for any bit of code, and they are especially useful for otherwise repetitive code. Another usage is "extracting" code in a bite-sized piece, either to make your code easier to read or because you can't in-line the code easily.
An example of the first case is displaying a message:
function showMessage(player, msg)
local gui = player.PlayerGui.ScreenGui.Message
gui.Text = msg
gui.Visible = true
wait(2)
gui.Visible = false
end
showMessage(game.Players.adark, "Here's an example argument!")
What makes this especially lucrative is you can make the entire message system different by just changing how this one function displays the given text, without having to touch any of the rest of the code.
An example of the second case, taking a factorial:
function factorial(n)
if n <= 0 or math.floor(n) ~= n then
return nil
end
for i = n, 1, -1 do
n = n*i
end
return n
end
print(factorial(5))
The simple 3-line loop could be inlined, but what happens if you make a mistake writing it? Having the code in only one area means any error will be evident every time the function is used, instead of just one or two of the ten or more times you used the factorial inline.
This function also displays the second aspect of functions: returning values. You can return any number of values, using the , operator (a comma) to separate them. factorial returns only one, but here's an example that returns multiple:
function returnMultiple()
return "alpha", "omega", 42
end
a, b, c = returnMultiple()
print(a)
print(b)
print(c)
What's useful but not always practical is chaining together multiple returns with multiple arguments:
function printThreeThings(a, b, c)
print(a)
print(b)
print(c)
end
function generateThreeNumbers()
return math.random(), math.random(), 7
end
printThreeThings(generateThreeNumbers())
Functions are considered first-class values, meaning they are the same as any other kind of data. What this means to you is that you can return functions as well!
function printBobFunction()
return function()
print("bob")
end
end
printBobFunction()()
The odd-looking function in there is called an anonymous function. You may be familiar with them in a slightly different case:
script.Parent.Touched:connect(function(hit)
print(hit)
end)
1 comment
This has helped me so much, its more than Knowledge...it's Truth.