How to Change a User's Balance?ΒΆ
Let's say in our bot we have a button called Claim
, and we want it to increase the user's balance by 1 when clicked.
The button would look like this:
Here's how it works: every time a user clicks the button, their balance
variable will increase by 1, and then we'll show them a message telling them that their balance has increased by 1, along with their total balance.
Keep in mind
Before we begin, it's important to mention that we need to have the variable to be modified created beforehand. The balance
variable comes pre-created with your bot by default. However, if you're going to modify a variable other than balance
, you'll need to create it. We recommend reading the section dedicated to variables by clicking here before proceeding.
Let's see step by step how to increase the balance variable:ΒΆ
- First, enter your bot's editor mode by clicking the
π Edit
button, and click the button you want to edit, in our case, it'sClaim
. Then click onActions π
to edit its actions. - Add the Save to variable action. This action is very important and you'll use it often, as it's responsible for saving and changing data in previously created variables.
- Click on
Configure π
to set up the action. -
Click on
π Select variable π
and send the name of the variable to modify, in our case, it'sbalance
. -
Now you can choose between
π’ Set π’
andπΊ Increment πΊ
. Let's see the difference between each one:- Set: used to set a fixed value in a variable, whether it's a number in a numeric variable, text in a text variable, or a date in a date variable, etc.
- Increment: exclusive to numeric variables, it's used to increase or decrease the value of a numeric variable.
In our case, as you might have guessed, we need to increment the
balance
. Therefore, click onπΊ Increment πΊ
. -
Now we need to specify the amount to increment. We've said we want to increase the
balance
variable by 1, so simply send 1.This way, when the button is clicked, the
balance
variable of the user who clicked the button will increase by 1. If we want to decrease it, we just need to put a negative number, like -1.We could also increment the variable not for the user who clicked the button, but for another user of the bot. For that, we have another button that has just appeared called
π€ To other user π€
. It's not necessary in this guide, but it's worth mentioning. -
Go back to the button's actions list and create a new one, this time it will be a Send message action, so we can send a message to the user telling them that their balance has changed, and click on
Configure π
to set it up. -
Save the message that will be sent to the user when they click the button, after their balance has been changed. In my case, I'll use the following:
Note that I've used an expression to show the value stored in the variable to the user. It's as simple as writing the variable's name in curly braces. In our case, the variable is named
balance
, so the expression to display its value in a message would be{balance}
. -
Go back and activate both actions.
And that's it! When the Claim
button is clicked, the balance
will increase by 1, and we'll show it to the user, it will look like this:
As you can see, I clicked the button 2 times, and it works!