what's the difference? I've seen "end)" used in some youtube tutorials but they don't explain why " )" is being used or what it does.
Some functions have arguments that are functions. An example of one of these is a method of an event, :connect(). It's used to connect events to the function you provide:
event:connect(function() --function argument --do stuff end )
You can see why the ) after the end is require if you pay attention to syntax , without it there will be an open ( without a close.
Another example that takes a function argument is spawn(). It makes a function run on a separate thread:
spawn(function() while wait(1) do --stuff end --notice this is doesn't have a ) because there is no open ( end )
Here's the difference between those two...
end
As you have probably seen some scripts, you've probably seen this nearly everywhere. In if
statements and in while
and for
loops this is put in the end. And also functions like the following. And probably in some other statements i may have forgotten about.
This is a script parented under a TextButton
:
function fart() local msg = Instance.new("Message", workspace) msg.Text = "You still find those jokes funny?" wait(3) msg:Destroy() end script.Parent.MouseButton1Click:connect(fart) --connect() performs the function provided inside when the event "fires" [i.e. occurs]. This event fires when the left mouse button fully clicks the TextButton (which has the event).
CAUTION! The repeat
loop has it's own unique 'ending' until
. In the same line until
is we put what statement or comparison would stop the loop if true. Never end those loops with end
! Why? Because... things won't end
well otherwise... Anyone? I'll just demonstrate a repeat
loop.
repeat wait(1) Instance.new("Part",workspace) until workspace.CheckMe == nil
end)
You see, this ending is not any different from end
. The parenthesis doesn't even belong to it. That's right, parenthesis, that is not your father. However, you DO have a father: The function which was connected!
What is this function? I will talk about that right away.
Take a look at the function i had demonstrated. Look at the bottom line. Don't you think i could just move my function inside the connect()
? If you did think that and were wondering, then yes, yes you can. Let's see what the script would look like if my fart()
function was directly inside the connect()
:
script.Parent.MouseButton1Click:connect(function()) local msg = Instance.new("Message", workspace) msg.Text = "You still find those jokes funny?" wait(3) msg:Destroy() end) --The ending of this topic!
We put the end)
into a similar position to end
for our own convenicence. Now, i should finally talk about the parenthesis in the end)
.
As you can see, the whole function is inside a parenthesis itself, and it must end somewhere. So, as the function would normally end in end
, we need not extend it any further - we close the parenthesis right after the end
! And after we do that, end)
is 'formed'.
That's just about it, in a formal way. I hope this clarifies things!