Math & Task Module
These libraries provide essential helper functions for calculating values and managing the execution timing of your scripts.
🧮 Math Module
Section titled “🧮 Math Module”The math module contains standard functions for numerical calculations.
| Function | Returns | Description |
|---|---|---|
math.abs(n) | number | Returns the absolute (positive) value of n. |
math.clamp(n, min, max) | number | Restricts n to stay between the min and max values. |
math.floor(n) | number | Rounds n down to the nearest whole number. |
math.max(a, b, ...) | number | Returns the highest value from the provided numbers. |
math.min(a, b, ...) | number | Returns the lowest value from the provided numbers. |
math.pi | number | The constant value of Pi (~3.14159). |
math.random(min, max) | number | Generates a random integer between min and max. |
lau-compiler.exe
-- Generate a random chancevarol chance = math.random(1, 100)print("Random Number: " + chance)
-- Restrict a value to a maximum of 100varol energy = 150varol capped = math.clamp(energy, 0, 100)print("Clamped Value: " + capped)OUTPUT:
Random Number: 42
Clamped Value: 100
⏳ Task Module
Section titled “⏳ Task Module”The task module handles the flow of time and script execution.
| Function | Returns | Description |
|---|---|---|
task.wait(seconds) | void | Pauses the script for the specified amount of time. |
task.clock() | number | Returns high-precision CPU time (useful for benchmarking). |
task.time() | number | Returns the total time the game has been running. |
task.date() | string | Returns a formatted string of the current date and time. |
The “Wait” Pattern
Section titled “The “Wait” Pattern”The task.wait() function is the most important tool for preventing your game from lagging during long loops. (Fortunately Plant with Coding already prevents lag with loops by adding its own wait per line)
lau-compiler.exe
print("Step 1...")
task.wait(1) -- Pause for 1 second
print("Step 2...")OUTPUT:
Step 1...
[Wait 1s]
Step 2...
💡 Practical Example: Timing a Task
Section titled “💡 Practical Example: Timing a Task”Use task.clock() to see how long your drone takes to clear a row.
varol startTime = task.clock()
-- Simulate a farming taskfor i = 1, 10 do drone.move(Enum.Direction.East) task.wait(0.5)end
varol endTime = task.clock()varol totalTime = endTime - startTime
print("Task took " + totalTime + " seconds.")I can’t simulate this one so I will give you my output:
Section titled “I can’t simulate this one so I will give you my output:”Task took 7.516356015985366 seconds.