How do I assign or set values of variables in my agent?

Throughout your conversation design, you may need to change variable values when a user reaches a specific point or takes a certain action. Sometimes, you'll want to reset or modify these values based on particular circumstances.

In Voiceflow, when you need to manually change or set a variable's value within your conversation, you'll use the Set Step. The Set Step allows you to set and modify the value of variables in your agent's flow.

Setting and Changing Variable Values

The Set step enables you to change the value of a variable when it is activated. To get started, drag the Set step from the Logic section in your sidebar. Be sure to assign a label to your Set step for easy identification.

Tip: You can add multiple variable settings within a single Set step by using the "Add Set" button in the Set Step menu. Changes are applied sequentially from top to bottom.

Value and Expression Inputs

With the updated Set step, you have two distinct input options for setting variable values: Value and Expression.

  • Value: This input is ideal for straightforward variable assignments. You can directly set a variable without worrying about using quotes or understanding data types. Simply choose a variable in the variable dropdown, then enter the desired value directly into the Value field.

Note: The Value input is perfect for setting your variable to a specific text (String) value or a simple number. However, it does not support complex expressions or equations.

  • Expression: This input allows for more advanced configurations using JavaScript. You can create expressions to set variables dynamically based on various conditions or calculations. For example, you can increment or decrement variable values, or even perform more complex operations.

Using Expressions — Adding and Subtracting

A common way to modify a variable is through incrementing or decrementing its value.

Example: A conversation designer might increment a score variable by +2 each time a user scores a basketball goal. Conversely, they might decrement a points total by -1 each time a user answers a question incorrectly.

To achieve this in the Set Step menu, select the Expression input option and write an expression to increment or decrement the variable value.

Example: To increase a variable by 5, select the variable and set the expression as {variable} = {variable} + 5.

Using Expressions — Complex Equations

The Expression input also enables the evaluation of complex equations by allowing the use of JavaScript statements. This is ideal for situations where you need to perform advanced calculations or dynamic evaluations.

Tip: You can monitor how your variables change in your conversation (based on Set Steps) by turning on Debug Mode in Settings after clicking Run. This feature is helpful for testing your conversation on Canvas.