A Roblox Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeLatest imagesRegisterLog 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
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
naknak
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
Supernapalm
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
m27frogy
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
slayer9365
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
myrco919
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
branefreez
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
ninga95
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
CloneTrooper787
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
raboy117
Explanation Of Anonymous Functions Vote_lcapExplanation Of Anonymous Functions Voting_barExplanation Of Anonymous Functions Vote_rcap 
Top posting users this month
No user
Latest topics
» Where to go from here.
Explanation Of Anonymous Functions EmptySeptember 14th 2020, 1:20 pm by MrNicNac

» Send me an Email
Explanation Of Anonymous Functions EmptySeptember 14th 2020, 1:16 pm by MrNicNac

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
Explanation Of Anonymous Functions EmptyJuly 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
Explanation Of Anonymous Functions EmptyJuly 6th 2015, 4:16 pm by m27frogy

» Ambassador!
Explanation Of Anonymous Functions EmptyApril 15th 2015, 11:40 pm by naknak

» Boop - Tag
Explanation Of Anonymous Functions EmptyApril 13th 2015, 9:46 pm by naknak

» Vip Class Script
Explanation Of Anonymous Functions EmptyApril 13th 2015, 4:54 pm by naknak

» Who's active?!
Explanation Of Anonymous Functions EmptyApril 13th 2015, 4:52 pm by naknak

» Genesis Point
Explanation Of Anonymous Functions EmptyJuly 17th 2014, 7:04 pm by branefreez

» Reward System
Explanation Of Anonymous Functions EmptyJuly 17th 2014, 5:41 am by m27frogy

» Script Request
Explanation Of Anonymous Functions EmptyJuly 10th 2014, 11:43 am by naknak

» local scripts?
Explanation Of Anonymous Functions EmptyJuly 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
Explanation Of Anonymous Functions EmptyJuly 10th 2014, 11:36 am by naknak

» Hi. I am new here
Explanation Of Anonymous Functions EmptyApril 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
Explanation Of Anonymous Functions EmptyJanuary 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
Explanation Of Anonymous Functions EmptyJanuary 1st 2014, 2:11 pm by m27frogy

» Gun Making! [READ DESC]
Explanation Of Anonymous Functions EmptyJanuary 1st 2014, 2:10 pm by m27frogy

» Hi, I am new here!
Explanation Of Anonymous Functions EmptyNovember 26th 2013, 3:33 pm by Keanu73

» Improve Coding
Explanation Of Anonymous Functions EmptyOctober 26th 2013, 1:12 pm by pook03

» Simple Button
Explanation Of Anonymous Functions EmptySeptember 1st 2013, 6:19 pm by branefreez


 

 Explanation Of Anonymous Functions

Go down 
3 posters
AuthorMessage
lukenuke75
Novice Scripter
Novice Scripter
lukenuke75


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

Explanation Of Anonymous Functions Empty
PostSubject: Explanation Of Anonymous Functions   Explanation Of Anonymous Functions EmptyAugust 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
lukenuke75


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

Explanation Of Anonymous Functions Empty
PostSubject: Re: Explanation Of Anonymous Functions   Explanation Of Anonymous Functions EmptyAugust 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
naknak


Posts : 878
Join date : 2010-07-30

Explanation Of Anonymous Functions Empty
PostSubject: Re: Explanation Of Anonymous Functions   Explanation Of Anonymous Functions EmptyAugust 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
lukenuke75


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

Explanation Of Anonymous Functions Empty
PostSubject: Re: Explanation Of Anonymous Functions   Explanation Of Anonymous Functions EmptyAugust 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
blueymaddog


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

Explanation Of Anonymous Functions Empty
PostSubject: Re: Explanation Of Anonymous Functions   Explanation Of Anonymous Functions EmptyNovember 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.
Back to top Go down
Sponsored content





Explanation Of Anonymous Functions Empty
PostSubject: Re: Explanation Of Anonymous Functions   Explanation Of Anonymous Functions Empty

Back to top Go down
 
Explanation Of Anonymous Functions
Back to top 
Page 1 of 1

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