📘 Roblox Lua: Math Functions Notes
Roblox uses Lua's math library, which gives you built-in tools for numbers, randomness, rounding, and more.
🧮 1. Basic Math Functions
| Function |
Example |
What It Does |
math.abs(x) |
math.abs(-5) → 5 |
Absolute value (no negatives) |
math.floor(x) |
math.floor(3.7) → 3 |
Rounds down to whole number |
math.ceil(x) |
math.ceil(2.1) → 3 |
Rounds up to whole number |
math.round(x) |
math.round(2.6) → 3 |
Rounds to nearest whole number |
math.max(a, b) |
math.max(5, 9) → 9 |
Returns the biggest number |
math.min(a, b) |
math.min(5, 9) → 5 |
Returns the smallest number |
math.clamp(x, min, max) |
math.clamp(12, 0, 10) → 10 |
Limits x between min and max |
🎲 2. Random Numbers
| Function |
Example |
Description |
math.random() |
math.random() |
Random number 0 to 1 |
math.random(n) |
math.random(5) |
Random 1 to n |
math.random(a, b) |
math.random(10, 20) |
Random between a and b |
💡 Great for: random damage, spawn locations, item drops.
📐 3. Trigonometry (for advanced stuff like rotations, circles)
| Function |
Example |
Description |
math.rad(x) |
math.rad(90) → 1.57 |
Degrees → Radians |
math.deg(x) |
math.deg(1.57) → 90 |
Radians → Degrees |
math.sin(x) |
math.sin(math.rad(30)) |
Sin of angle (used for motion) |
math.cos(x) |
math.cos(math.rad(60)) |
Cosine |
math.atan2(y, x) |
math.atan2(1, 1) |
Angle between 2 points |
✅ Summary Table
| Type |
Functions |
| Rounding |
math.floor, math.ceil, math.round |
| Value Limits |
math.max, math.min, math.clamp |
| Random |
math.random, math.random(a, b) |
| Trig/Rotation |
math.sin, math.cos, math.rad, math.deg |
| Distances |
(A - B).Magnitude |