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

what do the parenthesis in functions do ?

Asked by 5 years ago

like for example I see function Noob(Guest) I still don't get what those mean I know what functions do and I see the parameter being reused in a script I don't get how that works can anyone explain I tried to check the wiki already

1 answer

Log in to vote
4
Answered by 5 years ago

Parenthesis vs Brackets

() and [] mean different things in RBLX Lua. I'll describe them briefly now so you don't confuse [] with () later. Square brackets are operators. They work similarly to how the dot (.) operator works. You can get a value out of a table or a property of any object. If you know the basics of coding in Lua, you are probably already using the dot operator and maybe even the brackets without knowing they're operators.

workspace.Part.BrickColor --Getting the brick color property of the object "part" which is inside workspace
workspace["Part"]["BrickColor"]

local table = {["Dictionary"] = true, array = false}
while table["Dictionary"] == table.array do end

Function

Before you know what the parentheses do, I'll briefly explain a function. A function is a block of code that you can run over and over again whenever you call it. You call it by using the function name and then adding the parentheses at the end. Here are the different ways you start a function:

function a() --function a() can be local function a()
    --code
end

local a = function()
    --code
end

function() --Called an anonymous function because there is no name
    --code
end

--Called a method. A function that operates on the table that it is inside of.
local table = {}

function table:a()
    --code
end

Arguments and Parameters

Arguments and Parameters work hand in hand in a function. Lets say we're making a function a(). What it will do is run and print a message. We will call it after.

function a()
    print("Hello")
end

a()

We made a function and we called it. Lets say we want the script to say hello to someone specific. We can make a parameter called subject and add it to the function like so:

function a(subject)

Now, whenever someone calls the function a, whatever they add in between the parentheses, the argument, becomes "subject" or the parameter. It's like settings a variable, except specific to the function only.

local subject = "World"
--Subject is = to "World"


function a(subject)
    --Subject is = to "World" also because of line 10.
    print("Hello")
end

a("World")

We defined the parameter, subject, with argument "World".


Methods

I mentioned methods earlier. They use the colon (:) symbol. When you do this, function gets a parameter called self on default. self is the object calling the function. Here's an example of self in action:

obj:Destory() --A method; self is already defined by default
obj.Destroy(obj) --A function; parameter needs to be defined

local tab = {}
function tab:MyMethod() --Self is defined by default and no parameter is needed
    print("Called from "..tostring(self)) --Still works
end

Random Function Fun Fact

Predefined functions can be redefined as variables.

local PrintInYellow = warn

PrintInYellow("Hello World!")
warn("Hello World!")
--Since "PrintInYellow" equals warn, you can just swap words and it'll work the same way.

Hello World!



Hope it helps!

1
Sorry for any mistakes in stuff like grammar, I'm a bit tired. EzraNehemiah_TF2 3552 — 5y
Ad

Answer this question