Beginners Scripting Guide (BETA)
  • Getting Started
  • Basic Data Types
  • Variables
  • Basic global functions
  • Number Arithmetic & Compounds Assignment
  • If statement
  • Function
  • Table & Dictionary
  • Loops
  • Creating An Object
  • Understanding Client & Server
  • RemoteEvent
  • Unfinished & In-progress
    • Methods
    • RemoteFunction
    • Basic libraries
    • Basic Services
    • Basic of Vector3 and CFrame
    • Basic built-in events
Powered by GitBook
On this page
  • MAKING A FUNCTION
  • CALLING A FUNCTION
  • GLOBAL & LOCAL FUNCTION
  • GLOBAL
  • LOCAL
  • ADDING PARAMETERS TO FUNCTION
  • USING RETURN IN FUNCTION
  • RETURNING DATA BACK TO FUNCTION
  • RETURN A SUM TO FUNCTION
  • BREAKING A FUNCTION

Function

✏ Beginners Scripting Guide ✏

PreviousIf statementNextTable & Dictionary

Last updated 11 months ago

In this section, we will be discussing about function. A function is a code of block that can hold another code of block and can execute the code block that was stored in the function multiple times by using parentheses to call out the function.

MAKING A FUNCTION

Making a function is the same as making a variable. But it is just a little bit different. Instead of:

local myFunction

We can do it like this:

local function myFunction()

end

CALLING A FUNCTION

Now we have a function. But the function will not run yet unless you call it. To call it we will get it name and put parentheses after the name like this:

local function myFunction()
    print('Hello, World!!')
end

myFunction() -- Call the function to let it print "Hello, World!!" in the output

GLOBAL & LOCAL FUNCTION

A function also be GLOBAL and LOCAL. GLOBAL means it can be accessed everywhere in the script and LOCAL means it can be only accessed in a certain block of codes. It is the same as GLOBAL and LOCAL variable.

GLOBAL

if 3 > 1 then
     function sayHello()
          print('Hello, World!!')
     end
     sayHello() -- Hello, World!!
end

sayHello() -- Hello, World!!

LOCAL

if 3 > 1 then
     local function sayHello()
          print('Hello, World!!')
     end
     sayHello() -- Hello, World!!
end

sayHello() -- Error "attempt to index call nil" because the function is not global so say hello function is nil!!

ADDING PARAMETERS TO FUNCTION

Now let's say we want the function to print the total sum of 2 numbers that was given in the function when it was called. We can start doing that by making 2 parameters with different names like this:

local function myFunction(num1, num2)

end

Now we are adding 2 parameters together and printing it in the function:

local function myFunction(num1, num2)
    local result = num1 + num2
    print('The sum of '..num1..' and '..num2..' is: '..result)
end

Now we gonna call the function and pass in 2 arguments for the function:

local function myFunction(num1, num2)
    local result = num1 + num2
    print('The sum of '..num1..' and '..num2..' is: '..result)
end

myFunction(1, 1) --- The sum of 1 and 1 is: 2

USING RETURN IN FUNCTION

Return can be used to return data to the function or just break the function to prevent the code below from running.

RETURNING DATA BACK TO FUNCTION

To return data, we must make a function first and then use return and put the data that we want to return to the function after the return

RETURN A SUM TO FUNCTION

local function getSumOf1Plus1()
     return 1 + 1 -- returning the sum of 1 + 1 back to function.
end

local result = getSumOf1Plus1()
print('1 + 1 = '..result) -- 1 + 1 = 2

BREAKING A FUNCTION

local a = true

local function runAFunc()
	--- If a is false then return.
	if not a then
		return
	end
	--- Else continue the code below.
	
	print('Hello, World!')
end

a = false

local result = runAFunc()
print(result) --- nil since we return nothing when "a" is false in script.
https://create.roblox.com/docs/luau/functions#basic-functions
https://create.roblox.com/docs/scripting/events/remote#argument-limitations