Complex Calculations with Lua

An introduction to our scripting language, Lua, that allows you to add complex calculations to your form

Angela Lim avatar
Written by Angela Lim
Updated over a week ago

VerticalChange uses a scripting language called Lua in our Form Builder to perform complex calculations inside of a form.

While Lua is technically a programming language, you don't have to be a master coder to set up calculations in your forms. Below are some steps for how to set up your calculations, and some examples of calculations you can perform with Lua.

Set-Up

Before you start writing your calculations, you'll need an understanding of the different parts of the Form Builder that will be used.

  • Slugs: In order to reference particular fields in your calculations, each field will need to be assigned a slug. You can assign a slug by hovering over the field name, clicking to toggle settings, then entering in a unique, alphanumeric phrase into the Slug field.

  • Script: The Script text box is where you'll be typing in the calculations you'd like to use. It is located at the very bottom of the Standard Fields settings box.

Formatting

When writing your script, to refer to a field that you want to use in a calculation, you need to write out two parts:

  1. The word "answers", followed by a period .

  2. The slug's name

For example, if you have a numeric field with the slug 'assessment_score', if you want to use the number entered into this field to calculate a total at the end, you will refer to this field as:

answers.assessment_score

Script Examples

1. Arithmetic in Numeric Fields

You can use Lua to perform arithmetic calculations on numeric fields in your form.

For example, say you have two numeric fields, Number of Children and Number of Teachers, and you'd like another numeric field, Total, to automatically calculate the sum of the first two numeric fields.

You can automatically calculate the total by writing a script using the + operator.

answers.num_children + answers.num_teachers

The expression above adds the two numbers together, but we'll now need to tell our form which field the sum should be put into.

answers.total = answers.num_children + answers.num_teachers

In Lua, the operation will always be on the right side, and the field where the result of the operation should be calculated will be on the left side.

With this script, you can now enter in the Number of Children and the Number of Teachers, and see that the sum is auto-calculated in the Total field.

Below is a list of the arithmetic operators you can use in your calculations:

  • +  Adds two fields

  • -  Subtracts the second field from the first

  • *  Multiply both fields

  • / Divide 

  • ( ) Expressions wrapped in parentheses tell Lua to complete this expression first

Other Arithmetic Examples:

answers.income = answers.earned_income - answers.living_costs

answers.total_spaces_available = answers.number_spaces_available_per_room * answers.number_rooms

answers.average_age_in_family = (answers.child_1_age + answers.child_2_age + answer.child_3_age) / 3

2. If/Then Conditional Logic

Conditional logic allows you to calculate on fields only if a certain condition is true.

For example, say you are tracking a family's income, and you would like a single-text field to automatically indicate whether a family is above or below the poverty line, based on their income. 

You can write a conditional script that checks if the income is less than or equal to a particular number. If it is, set a text field to "Below Poverty Line"; if it is not, set the text field to "Above Poverty Line".

Formatting

The formatting for an if/then statement is as follows:

if conditional statement
then the action you want to take if the conditional statement above is true
else the action that you want to take if the conditional statement above is false
end

In our family income example, the formatting would look like:

if answers.family_income <= 22000
then answers.poverty_line = "Below Poverty Line"
else answers.poverty_line = "Above Poverty Line"
end

Filling out a form result on a family would then auto-calculate different responses in the "Poverty Line" field based on what the user enters:

Relational Operators
Below are the relational operators you can use in your conditional statements

  • == Checks if the value of two fields are equal

  • ~= Checks if the value of two fields are not equal

  • > Checks if the value of the field on the left is greater than the field on the right

  • < Checks if the value of the field on the left is less than the field on the right

  • >= Checks if the value of the field on the left is greater than or equal to the field on the right

  • <= Checks if the value of the field on the left is less than or equal to the field on the right

Even More!

There are so many things you can do with Lua, but the items above are some basic examples to get calculations and conditional logic into your form. 

With Lua, we also have the possibility to assign scores based on values in Likert/Multi-Select/Single-Select fields, set defaults for unanswered fields, and do internal calculations before assigning the value to a field in your form. 

To learn how to do more complex Lua calculations, reach out to your Customer Success Manager, or check out some of these online Lua help guides:

Did this answer your question?