Top posting users this month | |
Latest topics | » Where to go from here.September 14th 2020, 1:20 pm by MrNicNac» Send me an EmailSeptember 14th 2020, 1:16 pm by MrNicNac» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]July 6th 2015, 7:38 pm by m27frogy» New Site PossiblyJuly 6th 2015, 4:16 pm by m27frogy» Ambassador!April 15th 2015, 11:40 pm by naknak» Boop - TagApril 13th 2015, 9:46 pm by naknak» Vip Class ScriptApril 13th 2015, 4:54 pm by naknak» Who's active?!April 13th 2015, 4:52 pm by naknak» Genesis PointJuly 17th 2014, 7:04 pm by branefreez» Reward SystemJuly 17th 2014, 5:41 am by m27frogy» Script RequestJuly 10th 2014, 11:43 am by naknak» local scripts?July 10th 2014, 11:39 am by naknak» Project: Reconstruction [Died]July 10th 2014, 11:36 am by naknak» Hi. I am new hereApril 26th 2014, 4:01 pm by altshiftkey» What's your favorite sport?January 1st 2014, 2:13 pm by m27frogy» FlashLight ScriptJanuary 1st 2014, 2:11 pm by m27frogy» Gun Making! [READ DESC]January 1st 2014, 2:10 pm by m27frogy» Hi, I am new here!November 26th 2013, 3:33 pm by Keanu73» Improve CodingOctober 26th 2013, 1:12 pm by pook03» Simple ButtonSeptember 1st 2013, 6:19 pm by branefreez |
|
| Explanation Of Anonymous Functions | |
| | Author | Message |
---|
lukenuke75 Novice Scripter
Posts : 38 Join date : 2011-03-07 Age : 27
| Subject: Explanation Of Anonymous Functions August 13th 2011, 2:26 am | |
| One prime example of an anonymous function is: - Code:
-
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then hit.Parent:BreakJoints() end end) People make "Kill Bricks" quite often. Whether it has to do with an obstacle course, or a lava flood game. So one way of making the code shorter plus a lot more readable for most players, is to make it into an anonymous function. They function the same way as normal functions, just its built into the connect line. I understand why most get confused; An easy way to explain it would be, anonymous functions act like normal functions, but are compact into one single line and do not require a separate connection line, Therefore it saves time and space. Code Breakdown Now I am going to break this code down, so that you can learn off of this tutorial. - Code:
-
script.Parent.Touched:connect( As you may have realized, this line is the same as an ontouch connection line, that's because it is. This is where the hit parameter of the next portion of code comes from. It acts just like - Code:
-
script.Parent.touched:connect(onTouch) - Code:
-
function(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then hit.Parent:BreakJoints() end end) This portion of code basically is your function, it acts exactly like putting the function in your script, then calling it with a connection line. So, what you would do in your function, you would do here. Note: Do not add any sort of declaration E.g : - Code:
-
function onTouch(hit) It is useless in an anonymous function, and I believe, if I am not mistaken, it will return an error.
Last edited by lukenuke75 on August 13th 2011, 3:38 pm; edited 1 time in total | |
| | | lukenuke75 Novice Scripter
Posts : 38 Join date : 2011-03-07 Age : 27
| Subject: Re: Explanation Of Anonymous Functions August 13th 2011, 2:27 am | |
| Tutorial finished, Reserved for example scripts. This piece of code is fired when a player enters the game, It will check if lukenuke75 enters the game, if he does then it will loop through the children of his character and turn the transparency to 1. - Code:
-
game.Players.PlayerAdded(function(player) if player.Name == "lukenuke75" then for i,v in pairs(player.Character:GetChildren()) do if v.className == "Part" then v.Transparency = 1 end end end end) This piece of code is an example of how you can change the scripts parent when they enter(Very useful!) - Code:
-
game.Players.PlayerAdded(function() script.Parent = nil end)
It is just that simple.
Last edited by lukenuke75 on August 19th 2011, 1:04 am; edited 1 time in total | |
| | | naknak Administrator
Posts : 878 Join date : 2010-07-30
| Subject: Re: Explanation Of Anonymous Functions August 16th 2011, 7:48 pm | |
| Good tutorial. I use anonymous functions a lot, but sometimes for complex things it is impossible. | |
| | | lukenuke75 Novice Scripter
Posts : 38 Join date : 2011-03-07 Age : 27
| Subject: Re: Explanation Of Anonymous Functions August 19th 2011, 1:02 am | |
| - naknak wrote:
- Good tutorial. I use anonymous functions a lot, but sometimes for complex things it is impossible.
*cough* coroutines use them, people usually don't use actual functions for coroutines *cough* | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Explanation Of Anonymous Functions November 20th 2011, 6:04 am | |
| I only use anonymous functions when my code needs to act inside a function to run and I only need to use the code once in my script, eg: coroutine.wrap(function), string.dump(function) and object.event:connect(function)
thats when you should only use anonymous functions. | |
| | | Sponsored content
| Subject: Re: Explanation Of Anonymous Functions | |
| |
| | | | Explanation Of Anonymous Functions | |
|
| Permissions in this forum: | You cannot reply to topics in this forum
| |
| |
| Who is online? | In total there are 3 users online :: 0 Registered, 0 Hidden and 3 Guests
None
Most users ever online was 136 on October 21st 2024, 12:16 pm
|
| Please tell us what time zone you are in here
Got a really good idea? Post it in projects and it might show up here! |
|