Skip to content

Math & Task Module

These libraries provide essential helper functions for calculating values and managing the execution timing of your scripts.


The math module contains standard functions for numerical calculations.

FunctionReturnsDescription
math.abs(n)numberReturns the absolute (positive) value of n.
math.clamp(n, min, max)numberRestricts n to stay between the min and max values.
math.floor(n)numberRounds n down to the nearest whole number.
math.max(a, b, ...)numberReturns the highest value from the provided numbers.
math.min(a, b, ...)numberReturns the lowest value from the provided numbers.
math.pinumberThe constant value of Pi (~3.14159).
math.random(min, max)numberGenerates a random integer between min and max.
lau-compiler.exe
-- Generate a random chance
varol chance = math.random(1, 100)
print("Random Number: " + chance)
-- Restrict a value to a maximum of 100
varol energy = 150
varol capped = math.clamp(energy, 0, 100)
print("Clamped Value: " + capped)
OUTPUT:
Random Number: 42 Clamped Value: 100

The task module handles the flow of time and script execution.

FunctionReturnsDescription
task.wait(seconds)voidPauses the script for the specified amount of time.
task.clock()numberReturns high-precision CPU time (useful for benchmarking).
task.time()numberReturns the total time the game has been running.
task.date()stringReturns a formatted string of the current date and time.

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...

Use task.clock() to see how long your drone takes to clear a row.

varol startTime = task.clock()
-- Simulate a farming task
for 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.