vlekje513
|
  |
| Joined: 28 Dec 2010 |
| Total Posts: 9057 |
|
|
| 01 Jan 2014 06:29 PM |
To close functions right? Can somebody tell me why lua needs this to detect something, why we need to use it, and a good explenation? |
|
|
| Report Abuse |
|
|
smiley599
|
  |
| Joined: 23 Jan 2010 |
| Total Posts: 21869 |
|
|
| 01 Jan 2014 06:34 PM |
It is hard to explain. Let me try:
game.Players.PlayerAdded:connect(function(player) -Notice here there are three brackets, two ( brackets and one ) bracket. Where's the second ) bracket?
It goes at the end of the whole function, so: game.Players.PlayerAdded:connect(function(player) --code end)
The reason is because this is an anonymous function. It could be written: function newplayer(player) --notice one ( and one ) --code end game.Players.PlayerAdded:connect(newplayer) -- one ( and one )
Basically all anonymous functions' ends need a right bracket at the end of the end. lol |
|
|
| Report Abuse |
|
|
|
| 01 Jan 2014 06:37 PM |
As Smiley said, it's hard to explain. I'll try my best. ------------------------------- When you call the function, as soon as something happens, like game.Players.PlayerAdded:connect(function(player), they need to end with end). Other wise, if you call functions when you want, like function do_something(name), you need to use end. These functions are used in admin commands and things like this, as then you can 'goto' a line of code. It's hard to explain.
So, how to remember it? How I remember it, is typing it all on one line. Just look.
game.Players.PlayerAdded:connect(function(player) end) ^^^. ^^^ They make like an area to put the code in. The first bracket (before 'function') and the last one (after the 'end') is the sea to put the code it.
Sorry if this was a really bad explanation, I hope you understand. If you have any questions, just post them here, I'll reply.
Drrandomous
|
|
|
| Report Abuse |
|
|
vlekje513
|
  |
| Joined: 28 Dec 2010 |
| Total Posts: 9057 |
|
|
| 01 Jan 2014 06:37 PM |
| That really helped, but why write the code diffrent? |
|
|
| Report Abuse |
|
|
|
| 01 Jan 2014 06:39 PM |
Preference.
Like when people do .MouseButton1Down .MouseButton1Up It's just preference. |
|
|
| Report Abuse |
|
|
Azureous
|
  |
| Joined: 29 Jan 2012 |
| Total Posts: 25287 |
|
|
| 01 Jan 2014 06:39 PM |
It's a matter of how you utilize your functions and what events you want to occur.
In one instance you connect your function by ending it and connecting it to an event.
In another instance you connect it to an event first and then end the event and function together. |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 01 Jan 2014 06:39 PM |
You call also game.Players.PlayerAdded:connect(function() print'hi' end )
Ir is used to close the parenthesis that you have open. |
|
|
| Report Abuse |
|
|
vlekje513
|
  |
| Joined: 28 Dec 2010 |
| Total Posts: 9057 |
|
|
| 01 Jan 2014 06:40 PM |
Use function FUNCTIONNAME() Instead of :connect(function()? |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 01 Jan 2014 06:41 PM |
no :connect(() connects the event to the function
|
|
|
| Report Abuse |
|
|
smiley599
|
  |
| Joined: 23 Jan 2010 |
| Total Posts: 21869 |
|
|
| 01 Jan 2014 06:41 PM |
lol at our copy and pasting ;)
Why wrote the code different? The advantages of anonymous functions is you don't have to think of a name and it looks neater. The problem with it is only one connection like per function.
For example;
function noob() print'hi' end script.Parent.MouseButton1Down:connect(noob) --down for you @second replier (can't spell ur name) script.Parent.MouseButton2Down:connect(noob)
Now doing it as normal allowed me to put two connection lines for it, but if I had written it like:
script.Parent.MouseButton1Down:connect(function() print'hi' end)
I would have to write it again for the MouseButton2Down.
See? |
|
|
| Report Abuse |
|
|
vlekje513
|
  |
| Joined: 28 Dec 2010 |
| Total Posts: 9057 |
|
|
| 01 Jan 2014 06:44 PM |
Ok, thanks a lot! This helped me more than you expected, and you have explained it very well. Thanks. |
|
|
| Report Abuse |
|
|
smiley599
|
  |
| Joined: 23 Jan 2010 |
| Total Posts: 21869 |
|
|
| 01 Jan 2014 06:47 PM |
You're welcome. And thank you. :) |
|
|
| Report Abuse |
|
|