A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeFAQRegisterLog in
If you're a experienced coder make some tutorials! It would really help the site grow.
Make sure you read the rules(Which can be found by clicking here)
If you're a beginner at coding, try some tutorials.
We have many Moderators/Admins watching this site. Contact them with Questions.
Let us know what your favorite sport is. By clicking here to vote (Click here)
This site is becoming inactive. Lets make it active.
Log in
Username:
Password:
Log in automatically: 
:: I forgot my password
Top posters
blueymaddog
 
naknak
 
Supernapalm
 
m27frogy
 
slayer9365
 
myrco919
 
branefreez
 
ninga95
 
CloneTrooper787
 
raboy117
 
Top posting users this month
Latest topics
» Send me an Email
January 8th 2016, 10:14 am by naknak

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
July 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
July 6th 2015, 4:16 pm by m27frogy

» Ambassador!
April 15th 2015, 11:40 pm by naknak

» Boop - Tag
April 13th 2015, 9:46 pm by naknak

» Vip Class Script
April 13th 2015, 4:54 pm by naknak

» Who's active?!
April 13th 2015, 4:52 pm by naknak

» Where to go from here.
April 13th 2015, 4:51 pm by naknak

» Genesis Point
July 17th 2014, 7:04 pm by branefreez

» Reward System
July 17th 2014, 5:41 am by m27frogy

» Script Request
July 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 here
April 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
January 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
January 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 Coding
October 26th 2013, 1:12 pm by pook03

» Simple Button
September 1st 2013, 6:19 pm by branefreez


Share | 
 

 Explanation Of Anonymous Functions

Go down 
AuthorMessage
lukenuke75
Novice Scripter
Novice Scripter
avatar

Posts : 38
Join date : 2011-03-07
Age : 21

PostSubject: 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
Back to top Go down
lukenuke75
Novice Scripter
Novice Scripter
avatar

Posts : 38
Join date : 2011-03-07
Age : 21

PostSubject: 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
Back to top Go down
naknak
Administrator
Administrator
avatar

Posts : 878
Join date : 2010-07-30

PostSubject: 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.
Back to top Go down
lukenuke75
Novice Scripter
Novice Scripter
avatar

Posts : 38
Join date : 2011-03-07
Age : 21

PostSubject: 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*
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 19

PostSubject: 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.

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Sponsored content




PostSubject: Re: Explanation Of Anonymous Functions   

Back to top Go down
 
Explanation Of Anonymous Functions
Back to top 
Page 1 of 1
 Similar topics
-
» Outcry. The shimmering game
» urac wants my hide
» (FULL) Esamir International News Network
» An Explanation of Transwarp

Permissions in this forum:You cannot reply to topics in this forum
A Roblox Community :: Tutorials and Resources :: Intermediate-
Jump to: