Skip to content

Functions & Modules

Functions allow you to group code into a single command that can be used repeatedly. Modules allow you to share that code across different script files.


Functions are blocks of code that perform a specific task and can be used repeatedly. In the Lau language, functions can be defined in two different ways.

This is the most common method. You give the function a name and call it by that name wherever you need it.

A function is defined using the func keyword. You can pass information into them (parameters) and get information back (return).

lau-compiler.exe
func calculatePrice(price, amount)
return price * amount
end
varol order1 = calculatePrice(15, 10)
varol order2 = calculatePrice(20, 20)
print("Total: " + order1)
print("Total: " + order2)
OUTPUT:
Total: 150 Total: 400

LAU functions can return more than one piece of data at the same time.

lau-compiler.exe
func dronePosition()
return drone.getPositionX(), drone.getPositionZ()
end
varol hX, hZ = dronePosition()
print("X: " + hX + ", Z: " + hZ)
OUTPUT:
X: 0, Z: 0

You can assign a function to a variable, just like a number or a string. This method is very powerful for creating dynamic structures.

lau-compiler.exe
varol multiply = func(x, y)
return x * y
end
print(multiply(5, 2)) -- Prints 10
OUTPUT:
10

The return keyword sends the result of a function outwards. The Lau language supports returning multiple values from a single function at the same time.

  • Multiple Returns: You can send multiple pieces of data by separating them with commas, like return result1, result2.
  • Flow Control: The moment a function reaches a return line, it finishes its execution and exits.
lau-compiler.exe
-- 1. Standard (Single) Return
func square(number)
return number * number
end
varol result = square(4)
print(result) -- Prints 16
-- 2. Multiple Return (Special Lau Support)
func generateCoordinates()
varol newX = 2
varol newZ = 5
return newX, newZ -- Send by separating with commas
end
-- Let's catch the returned values with multiple assignment
varol x, z = generateCoordinates()
print("Generated X and Z: " + x + " " + z) -- 2, 5
-- 3. Ending the Function Early
func check(value)
if value < 0 then
return "ERROR: Negative number!" -- The function ends here
end
return "Number is Valid."
end
OUTPUT:
16 Generated X and Z: 2 5
  • Standard (func): The cleanest and most readable method for general operations.
  • Variable (varol): Used to put a function inside a list or to send it as a parameter to another function.

As your drone script grows, your code will become complex and long. The Module System helps you stay organized by splitting code into multiple files.

In the Lau system, you can create two types of files:

  1. .lau (Main Script): The brain of the Drone. It gives commands and is executed directly with the ‘RUN’ button.
  2. .laum (Lau Module): A helper library. It doesn’t do anything on its own; it only runs when called (req) by the main script.

Analogy: Think of the .lau file as a ‘Craftsman’, and the .laum file as the craftsman’s ‘Toolbox’. A toolbox cannot repair a house by itself; the craftsman needs to open that bag (req) and take the tools out from inside.

The most critical rule you must follow when creating a .laum file is this:

Example Module Structure (calculator.laum)

varol calculator = {} -- Create an empty list (box)
-- Put a function inside the box
calculator.add = func(a, b)
return a + b
end
calculator.pi = 3.14 -- Put a constant number inside the box
return calculator -- AND THE MOST IMPORTANT PART: Send the box out!

To use a module you’ve prepared in your main script, you must use the req function. By assigning the list returned by the module to a variable, you can access everything inside it.

Using a Module (main.lau)

lau-compiler.exe
-- Load the module into memory and assign it to the 'calc' variable
varol calc = req("calculator.laum")
-- Now we can use everything inside the module!
print(calc.add(10, 20)) -- 30
print(calc.pi) -- 3.14
OUTPUT:
30 3.14

Events are listeners which activate a function when a certain action is made. Some Events use a variable as a permanent input for data, while others only take input a single time.

When writing an Event into a program, it has three parts: the event to listen for, how to use the input, and the function to use the input in.

player.chatted:connect(func(message)
-- code here
end)
  • player.chatted : Listens for when the player chats
  • :connect : Ensures the Event listens permanently
  • func(message) end : Runs the contents of the function with the message in chat

There are many Events to listen for in different packages, but the way you listen to them is important for algorithms. There are two ways to listen to an Event, denoted by the word after the colon in the Event signature.

MethodDescription
:connect()Runs the function every time the event happens.
:once()Runs the function only the first time the event happens.
:disconnect()Stops the script from listening to the event.

Example - :connect() (Every time)

player.chatted:connect(func(item)
if (string.match(item, "Apple")) then
market.buySeed(Enum.Seed.Apple)
end
end)

This code will buy an item from the market every time the player messages the corresponding item name.

Example - :once() (One time)

player.chatted:once(func(item)
if (string.match(item, "Apple")) then
market.buySeed(Enum.Seed.Apple)
end
end)

This code will not cause issues. Once the player messages what item they want to buy, the Event will stop listening.

When you use :connect(), it creates a ‘Connection’ data type. If you assign this to a variable, you can manually stop listening to the event later using the :disconnect() command.

This is extremely useful for temporarily listening to an event, or breaking a connection to save memory and prevent unwanted triggers after a certain condition is met.

-- Assigning the connection to a variable
varol connection = player.chatted:connect(func(message)
print("Chat: " + message)
end)
-- Wait for 10 seconds, then stop listening completely
task.wait(10)
connection:disconnect()
-- Note: The 'once' command automatically destroys its own connection after running.