Strings and Math
Lua has built-in functions for working with text and numbers. Here are the ones you are likely to need for a HUD.
Converting values
Section titled “Converting values”tostring() turns a value into text. tonumber() tries to turn numeric text into a number:
lh.drawln("Visible: " .. tostring(true)) -- Output: Visible: true
local amount = tonumber("12.5")lh.drawln(amount + 1) -- Output: 13.5If conversion fails, tonumber() returns nil. Check before using the result:
local amount = tonumber("not a number")
if amount ~= nil then lh.drawln(amount * 2)else lh.drawln("Please enter a number.")endFormatting text
Section titled “Formatting text”string.format() fills placeholders in a string:
local fps = 120local x = 12.345
lh.drawln(string.format("FPS: %d | X: %.1f", fps, x))-- Output: FPS: 120 | X: 12.3| Placeholder | Meaning |
|---|---|
%s |
Text |
%d |
Integer |
%.1f |
Number with one decimal place |
%.2f |
Number with two decimal places |
%% |
A literal percent sign |
Pass an integer to %d. Use a decimal format like %.1f for fractional values. Formatting changes the displayed text, not the original number.
Useful string functions
Section titled “Useful string functions”local text = "Hello, LuaHUD!"
lh.drawln(string.upper(text)) -- Output: HELLO, LUAHUD!lh.drawln(string.lower(text)) -- Output: hello, luahud!lh.drawln(string.sub(text, 1, 5)) -- Output: Hellolocal start = string.find(text, "LuaHUD", 1, true)lh.drawln(start) -- Output: 8string.sub() includes both endpoints. string.find() returns the start and end positions, or nil if the text is missing. Assigning it to one variable keeps only the first result. Its fourth argument, true, makes it search for literal text instead of a Lua pattern.
These positions count bytes, so avoid cutting through characters that use multiple bytes.
Useful math functions
Section titled “Useful math functions”| Expression | Result | Meaning |
|---|---|---|
math.floor(12.8) |
12 |
Round down |
math.ceil(12.2) |
13 |
Round up |
math.abs(-5) |
5 |
Absolute value |
math.min(10, 20) |
10 |
Smaller value |
math.max(10, 20) |
20 |
Larger value |
Rounding down goes toward negative infinity: math.floor(-1.2) is -2.
To keep a number inside a range, combine min and max:
local percentage = 125percentage = math.max(0, math.min(100, percentage))lh.drawln(percentage) -- Output: 100