I use this in some scripts but i want to know the meaning of it. Like
Testing.MouseButton1Down:connect()
And why do we put some thing inside the braket? I dont put anything but i see some script does. Please tell me what does it mean. Thank you for reading or answering :)
In Lua we use ()
s to define and call functions. For example,
function printName() -- the ()s are used when defining a function print("Name: Walter White") end printName() -- also used when calling the function
> Name: Walter White
Since you're also wondering about arguments and parameters-
function printName(name) -- still used here, but now we have a parameter called 'name' print("Name: " .. name) end printName("Jesse Pinkman") -- again, still used here, but now we pass it an argument, "Jesse Pinkman"
> Name: Jesse Pinkman
In your example case, MouseButton1Down
was an event, I'd suggest reading the wiki page on them.
Parenthesis serve two functions in Lua, both of which are also found in mathematics with this notation!
1. Grouping operations, to evaluated in an order other than PEMDAS, e.g., x * (y + a) is not the same as (x * y) + a.
2. Function application, like in sin(x)
in mathematics. Lua uses the same convention of <functionname>(<functionparameters>)
. Juxtaposing a value with parenthesis in Lua always means function application, not multiplication:
(x+1)(x+2)
does NOT mean (x+1)*(x+2)
. It means apply (x+2)
to the function (x+1)
(which would only run without errors with strange metamethods).
They also appear in definitions of functions, although in that case it's a syntactic structure rather than actual application!
In the specific example you gave, we call that a method call instead of a function call because it acts slightly different, but operation is almost exactly the same:
a:b(c)
is the same thing as a.b(a,c)
(which is a regular function call).
The first part that you see before the ':' is the action. For example, script.Parent.ClickDetector.MouseClick. Then you stick :connect() behind it. The thing that goes in the brackets is a function that you made. So the finished thing would be this, if 'rek' was my function. script.Parent.ClickDetector.MouseClick:connect(rek)