|
| 03 Jun 2012 12:14 PM |
i dont really get how functions work. and the wiki aint helping me either.
function ontouch () can i name it something else besides ontouch. and how do arguments work?
~Look its Thembunnies! Nope, its just Chuck Testa~ |
|
|
| Report Abuse |
|
|
|
| 03 Jun 2012 12:17 PM |
Yes, functions can be named anything you want them to!: function YOUR_FUNCTION_NAME_HERE(argument_1, argument_2)
As for arguments, they're just variables for the function to use.
function printstuff(arg1, arg2) print(arg1, arg2) end printstuff("hey", "hi")
Describing functions goes too deep to explain since they can be used in so many ways. Try learning through experience. |
|
|
| Report Abuse |
|
|
|
| 03 Jun 2012 12:19 PM |
thanks that helped a lot!
~Look its Thembunnies! Nope, its just Chuck Testa~ |
|
|
| Report Abuse |
|
|
|
| 03 Jun 2012 12:21 PM |
You can name a function whatever you want, it's the connection line that matters. The reason people use things like OnTouch(hit) is because it is easier to read and understand, and more common.
Functions work like this. As I said before you can name a function whatever you want. Let's make a function called ducks()
function ducks()
Now, we have the start of our function. What comes next is what we want our function to perform.
function ducks()
print("Ducks!")
We want our function to print 'Ducks!'. Now, after this we need to end it so the function knows when to stop.
function ducks()
print("Ducks!")
end
This is the important part. We have what we want our function to do, but now we need to tell it when to do it. That is called the connection line. For something simple like this function we can make a connection like as simple as just ducks().
function ducks()
print("Ducks!")
end
ducks()
Now, certain actions need certain connection lines. OnTouched events need something like:
script.Parent.Touched:connect([functionnameherre]) |
|
|
| Report Abuse |
|
|
|
| 03 Jun 2012 12:22 PM |
| Oopsies. I was a little late. I also forget to explain the arguments part, I really only explained the connection line part, but you get the jiff. The guy that first posted explained it well. Hope I helped :b |
|
|
| Report Abuse |
|
|
3lex33
|
  |
| Joined: 08 Oct 2008 |
| Total Posts: 5220 |
|
|
| 03 Jun 2012 12:23 PM |
It is kind of knowledge, which will help you in any programming language, so you better understand it now. Function is a container for piece of code, which can be called and which can or can not return something instead. To make function do:
function *functionname*(arguments) --code end
After you can call it by:
*functionname*(arguments)
Now, how arguments work. Lets say you have:
function up(num) num = num+1 end
This will increase the argument which we pass in function. You can call function by doing:
up(4) --this will increase 4 to 5, but you wont notice it
a = 4 up(a) print(a) --this returns 5 ============= Now, roblox part. Roblox have built in events, which have built in arguments. You can see about it on wiki, so i will give one example:
function ontouched(hit) --We make function called ontouched and allow 1 argument
print(hit.Name) end
part.Touched:connect(ontouched) --This is where roblox starts doing work. Touched is built in event, which starts function when someone touched part and already pass this part as argument. |
|
|
| Report Abuse |
|
|
jode6543
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 5363 |
|
|
| 03 Jun 2012 12:29 PM |
A function is just a snippet of code that can be executed (commonly called "calling" the function) multiple times. Example:
--Here is the function declaration... function kickAll() local players = game.Players:GetPlayers() for i = 1, #players do players[i]:Destroy() end end
--Here is calling it... kickAll() wait(5) --... And here is calling it again. kickAll()
Would you rather retype the code in the function twice, or type it once with a function and have it executed twice with two calls to it?
Parameters/arguments are just variables that get "passed in" to the function's scope. (http://wiki.roblox.com/index.php/Scope) It is best shown with an example:
function add(a,b) print(a + b) end
add(1,1) --2 add(6,6) --12 add(2,2) --4
Lastly, returns. Returns are something that the function call brings back to the scope it was called from. Another example:
function add(a,b) return a + b end
local num1 = add(1,1) print(num1) --2 print(add(5,5)) --10
Hope this helps.
-Jode |
|
|
| Report Abuse |
|
|
jode6543
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 5363 |
|
| |
|
|
| 03 Jun 2012 12:54 PM |
why wont this work?
function coloring () script.Parent.BrickColor =Lime green wait(0.5) script.Parent.BrickColor = Really black wait(0.5) script.Parent.BrickColor = Royal purple wait(0.5) end coloring ()
~Look its Thembunnies! Nope, its just Chuck Testa~ |
|
|
| Report Abuse |
|
|
3lex33
|
  |
| Joined: 08 Oct 2008 |
| Total Posts: 5220 |
|
|
| 04 Jun 2012 05:10 PM |
Because roblox brickcolor works over way:
script.Parent.BrickColor = BrickColor.new("Name") script.Parent.BrickColor = BrickColor.new(color code)
I expect another wave of scripters explaining about functions :) |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 05:13 PM |
You put a space I think...
function onTouch() not function onTouch () |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 05:35 PM |
| Nope, it can work with a space also. |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 05:40 PM |
| Oh ok, I thought if you have a space you need a _ |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 05:43 PM |
Only if it's in the tag, I.E.
function on Touch(args) -> function on_Touch(args) function onTouch (args) -> correct |
|
|
| Report Abuse |
|
|