generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: First steps: Functions and Events

Previous Thread :: Next Thread 
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
20 Sep 2016 09:45 AM
I am making this for the newbie scripters only, and for those who are actually intrested in reading this.

Here I will try to explain, in a simple way, how functions and events work. Some of the information here, of course, is taken from the wiki and I'll try to simplify it the best as I can.

--------------------------------------------------------------------------------

1. Functions

Functions are ###o#e#####sed between the statements "function" and "end". Here is a sample of the function:

function MyFunctionName(Arguements)
-- Code
end

The purpose of functions is to save us time, for an example, if we want to check the name of the player, everytime a new player joins the game. They represent, to be said, a block of a code that is ran when a function is "called" by an event or via the script itself. "Calling" a function means like telling the function to run. To call a function we write the function's name, followed by opening and closing bracket:

MyFunctionName()

Inside the brackets of the function we can put arguements, the arguements can be anything that the script recognises, like a number, a part or a text. Here is an example function that will print out the given text:

function MyFunction(MyText) -- MyText now is equal to the value we gave below - "I like pizza!"
print(MyText) -- Prints "I like pizza!"
end

MyFunction("I like pizza!") -- Here we call the function with the text arguement

A function can also be given a value, with the "return" statement we give the function a value representing the value before the return statement. Here is an example of how "return" works:

function MyNewText(Text)
return Text -- Here we tell that the function has now the value of the arguement "Text"
end

Variable1 = MyNewText("Crossroads") -- Since return will make our function a value, we now add a variable that represents our function.
print(Variable1) -- Will print Crossroads, because the Variable1 will no longer be equal to MyNewText("Crossroads"), but to "Crossroads" or the "Text" value in the function.

Of course functions aren't meant to always be used with arguements, you can call a function without arguements if it doesn't have or doesn't need any:

function Print()
print("Floor") -- Prints "Floor" to the output
end

Print() -- Calling the function

--------------------------------------------------------------------------------

2. Events

Events are used when we want to run a code after something happens, they are used with functions to close the code that they'll run. Different events have different purposes, for an example, the "Changed" event will run after a property of a given object changes it's value or the event "Touched" which runs after a given object collides with another object.

The event is given after the object that uses the given event, the "Touched" event is used by parts only, while some other events like "PlayerAdded" are used by the "Players" service, here is an example:

workspace.Part.Touched:connect()

game.Players.PlayerAdded:connect()

The event is followed by the method named ":connect" and brackets, inside the brackets is our function that we have listed before the event, in the above example there are no functions for the events to run, which means they cannot work. So that means that we can call an existing function using an event. To do that we have to give the function's name, inside the evenet's brackets, followed by two brackets describing the arguements that the function may have. Inside the brackets of the function there is/are the arguement/s of the event, for an example, the "Touched" event has 1 arguement that refers to the part that touched our part, some events have more arguements, some none. Here is an example:

function PartTouched(Part) -- Refers to the object that touched our part
print(Part.Name) -- Prints out the name of the object
end

workspace.TestEvent.Touched:connect(PartTouched(ThePartThatHitMe))

"ThePartThatHitMe" refers to the the part that hit our part named "TestEvent" inside workspace, this arguement is given by the event, not by the author of the script. The arguement's name in the event and inside the actual function can be different in name.

--------------------------------------------------------------------------------

This is all from this guide, I hope I've helped you!
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
20 Sep 2016 09:47 AM
only one censorship, i can claim this as a victory

censored part: Functions are representing a code closed between
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
20 Sep 2016 10:00 AM
you never taught vararg or methods


local MyTable = {}
function MyTable:thing(arg1,arg2)
print(arg1,arg2)
end
MyTable:thing("hi ","there")

methods also receive 'self' which is the table using that function
useful when using metatables

local Object = {Name = "wow"}
function Object:PrintName()
print(self.Name)
end

Object:PrintName()

and vararg allows multiple arguments to be received

function println(...)
for _,arg in next,{...} do
print(arg)
end
end

println("test","of","using","vararg")

it can be used after any arguments or by itself

function dothing(first,second,...)
warn(first .. " " .. second)
print(table.concat({...}," "))
end

dothing("hello","boi","this","is","nice")
Report Abuse
LockFocus is not online. LockFocus
Joined: 31 Aug 2013
Total Posts: 1044
20 Sep 2016 10:02 AM
I forgot about 'self'
realizes mistakes
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
20 Sep 2016 10:03 AM
welp, I got shuffeled to simple stuff and forgot about them
Report Abuse
iIikeyou is not online. iIikeyou
Joined: 07 Mar 2012
Total Posts: 1659
20 Sep 2016 10:05 AM
if you're talking simple i think you forgot value types, arguments, and bare syntax for doing things
Report Abuse
iIikeyou is not online. iIikeyou
Joined: 07 Mar 2012
Total Posts: 1659
20 Sep 2016 10:06 AM
not arguments, operators
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
20 Sep 2016 10:08 AM
i am sure i said that you can use different values for arguements

this is suppoused to give you the base knowledge of functions and events, not rocket-fly you to the moon; first steps
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
20 Sep 2016 04:12 PM
This is a horrible tutorial. Not good for "first steps" and also full of horrible information.

Please, don't try again.
Report Abuse
Crimsonal is not online. Crimsonal
Joined: 23 Apr 2011
Total Posts: 1795
20 Sep 2016 04:14 PM
this is pretty good tutorial
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
20 Sep 2016 04:16 PM
You never talked about anonymous functions

MyFunction = function(var)
print(var)
end

MyFunction('hi') >> hi


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
20 Sep 2016 04:19 PM
"this is pretty good tutorial"
i get that you're trolling but you're still stupid

TimeTicks in Lua:
x = function() ... end
and
function x() end

are the exact same. Technically all functions are anonymous.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
20 Sep 2016 04:20 PM
I understand. It's simply syntactic sugar.


Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
20 Sep 2016 04:21 PM
^ that is just another way to declare functions

a specific example is this

pcall(function()
print("hi")
end)

where the function in unnamed and just used to be called in the pcall
Report Abuse
Crimsonal is not online. Crimsonal
Joined: 23 Apr 2011
Total Posts: 1795
20 Sep 2016 04:22 PM
my opinion. and why doesn't it surprise me that you go ahead and take the time to call me stupid
Report Abuse
iIikeyou is not online. iIikeyou
Joined: 07 Mar 2012
Total Posts: 1659
20 Sep 2016 04:40 PM
functions are just another value type, all you have to explain is the syntax of how to set one up and how to call it and how arguments turn into parameters
then i guess you could explain the uses of a function, e.g. being able to use the same chunk w/o rewriting, scopes, reciprocation

functions do nothing without operation, so teaching those operators and the very basic syntax of lua is the first step
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
21 Sep 2016 07:47 AM
@cntkillme1

"This is a horrible tutorial. Not good for "first steps" and also full of horrible information.

Please, don't try again."

your opinion (aka idk)

i see you troll but please do it less obvious, or at least try
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
21 Sep 2016 08:51 AM
I'm not trolling, you're just awful.
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
21 Sep 2016 09:21 AM
i can easily say you are an awful person, but two mean comments can't prove that

same goes to my thread, you can't just say it's bad with no arguements without sounding as a troll
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
21 Sep 2016 09:35 AM
I'll quote everything wrong with it when I get home.
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
21 Sep 2016 09:40 AM
from a friendly help thread it went to mass hate and disagreement

no wonder why people don't make guides, nobody cares to say thanks

here is your guide: http://wiki.roblox.com/index.php?title=Special:RobloxLandingPage
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
21 Sep 2016 10:24 AM
Don't resort to emotional arguments please.
I don't have a problem with people writing tutorials, in fact I'm all for it. I just have a problem when people write things on something they know nothing on AND publish it. That spreads misinformation, and especially as a beginner it's easy to believe everything you read about what you're learning.

"The purpose of functions is to save us time"
That's misleading and vague. It doesn't "save time" (whatever that is supposed to mean), its purpose is for organization and code reuse.

"Inside the brackets of the function we can put arguements, the arguements can be anything that the script recognises, like a number, a part or a text."
Very misleading. One subtle point is that anything inside the brackets of the function are called parameters, not arguments. Arguments are the values themselves you pass. Anyways the major problem is the last phrase. "the [arguments] can be anything that the script recognises" literally makes no sense. The "script recognises" keywords and operators but you can't pass them as arguments.

"A function can also be given a value, with the 'return' statement we give the function a value representing the value before the return statement."
Nope, a function _is_ a value, returning something from the function doesn't change that. Anything you return is simply returned when you call the function. We don't "give the function a value representing the value before the return statement" because, well, firstly that makes no sense, and secondly even if it did make sense, we're not giving a function a value.

"Events are used when we want to run a code after something happens, they are used with functions to close the code that they'll run."
This part is okay I guess, except the last part. "close the code" what?

"The event is given after the object that uses the given event, the 'Touched' event is used by parts only, while some other events like 'PlayerAdded' are used by the 'Players' service, here is an example:"
None of this makes any sense. I get what you're trying to say but you're saying it horribly.

"The event is followed by the method named ':connect' and brackets, inside the brackets is our function that we have listed before the event" There is also a 'wait' method of events. And your example is completely wrong lmao
"workspace.TestEvent.Touched:connect(PartTouched(ThePartThatHitMe)) "
Really? You don't even understand events why are you trying to teach them.

You didn't help anyone.
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
21 Sep 2016 10:34 AM
my good sir, understanding ~= explaining, yes i tried to simplify it for them, yes i may have done that rough, but the wiki is full of your accurate statements like you said "omg u used arguements instead of parameters u domb sh"

but anyways, i am not going to make a tutorial again, not because of you (don't get confident now, lol), because there is nobody in this scripters forum worth my time to write a simplified version of the tutorials given in the wiki, i hope i can now not mislead anyone with my crappy guides

bye.
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
21 Sep 2016 10:37 AM
"nobody in this scripters forum worth my time"
You mean being called out on your stupid statements is beneath you Mr. Untouchable?
Get out.
Report Abuse
Mitko0o1 is not online. Mitko0o1
Joined: 30 Nov 2010
Total Posts: 5725
21 Sep 2016 10:39 AM
haha, you are not the one to tell me to get out

if anyone should get out is you and your negativity, we don't need them

>bye.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image