Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Functions Help?

Asked by 8 years ago

Could someone like lay out the "rules" for functions, because I do not understand the part about how some functions are allowed to have parameters and others cannot. Also I do not understand the set functions that ROBLOX already has to supply users with. I'd really like help with these because this is the main area where I get confused with in coding and if I'm able to come over this obstacle I believe that I'd be able to continue scripting and further my experience. Plus, functions are one of the most important parts to scripting on ROBLOX. Thanks.

(Sorry if this wasn't a direct scripting question)

2 answers

Log in to vote
0
Answered by 8 years ago

Functions are something which provides a movement through out something.

function Name(reference) 
-- do stuff 
end
script.Parent.Touched:connect(Name)

so the Function, you can name it anything like Lawrence,Morgan anything you want. The Reference as well, you can name it, but as usual you can leave that blank. do stuff is where you're going to fulfill anything that you want to happen when the script.Parent is touched, as seen below the 'end' is the event, you aren't allowed to name it anything you want script.Parent identifies the location and Touched is the main event, you aren't allowed to make it Madness,Slowness you are just allowed to use ROBLOX's Options for any compatible event to the item you're referring to. Then the (Name) Just put that the name of your function (inside the parenthesis) There is also another way of making a function:

script.Parent.Touched:connect(function(NAME)
--do stuff
end)

as you can see instead of putting the event at the bottom, you put it IMMEDIATELY in the top and the Function is enclose through the parenthesis and you can add any name in it. the end is very different as well.

PLEASE NOTE THAT YOU CAN ONLY USE THE NAME OF THE FUNCTION 1 TIME ONLY. If you did, the function will connect to each other..

Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

A function is a procedure. You take things in, do stuff, and put things out. There is a function definition where you say how it works. That will decide which parameters you take in.

Looking at documentation

Reading documentation is very important to programmers!

If you want to know a function built into ROBLOX, you can look at the Wiki.

For example, you can look at the documentation for table.insert.

table.insert(table, [pos,] value) tells you how to use it. The first parameter is table, the second parameter (which is optional -- that's what the [] mean) is the position, and the third parameter is the value.

The description below tells you what it does and how to use it:

Inserts element value at position pos in table, shifting up other elements to open space, if necessary. The default value for pos is n+1, where n is the length of the table (see ยง2.5.5), so that a call table.insert(t,x) inserts x at the end of table t.

And it includes example code.

Methods

Methods are functions that act on a particular object. They usually use : instead of .. You can look up the methods available on the type of object from the Wiki. For example, we can look up what a Player has.

We can see the following line under "Functions":

void Kick ( string message = "" ) Removes a player from the game by disconnecting their client.

And you can click "Kick" to get a more detailed view.

void Kick( string message = "" )

This tells you it returns void (nothing), it takes in a string called message, which defaults to "" (empty text).

Defining Functions

When you define a function, you give it the block of code it executes:

function sayHi()
    print("Hello")
end
sayHi()

If your function needs some information to decide what to do, it can take in parameters. You put the names of the parameters in the (), and then those variables can be used in the body of the function:

function sayHiTo(person)
    print("Hello", person)
end
sayHi("BlueTaslem")

When you call the function (use it), you line up the parameters in the function definition with the values going in.

For example, in

function add(a, b, c)
    return a + b + c
end

print( add(1, 2, 3) )

a is 1; b is 2; c is 3.

Normally, the number of parameters in the function definition and where you call it will be the same.

If you provide more arguments than the function defined, they are ignored:

function add(a, b)
    return a + b
end
print( add(5, 1, "cat") ) -- "cat" is ignored

If you provide fewer, they are nil in the body of the function:

sayHiTo()
-- Hello nil

This can be useful for making "optional" parameters:

function sayHiTo(person)
    if person == nil then
        print("Hello Bob")
    else
        print("Hello", person)
    end
end
sayHiTo()
-- Hello Bob

Answer this question