Collect reward
In this guide, you will learn how to create a one-time use command that allows users to collect a money reward.
1. Create a command
The first step is to create a new command, which you can learn to do in this guide.
2. Set a trigger
Users will collect their reward by using the !collect
command, so let's set that up as the trigger:
3. Craft the code
Now that we have the initial setup done, it's time to make the command actually work.
We will use:
- $getUserVar - to load the user's balance
- $let - to temporarily store the balance
- $math - to calculate new balance
- $setUserVar - to set the new balance
Prize
The reward that users will receive is 100$ with the template economy. Since the template economy uses the money
user var to store a user's balance, we will contribute to that var.
Getting user var
Let's get a user's balance and store it in a temporary variable called bal
:
$let[bal;$getUserVar[money]]
Calculating new balance
Next, we'll add 100$ to the balance:
$let[bal;$math[$bal+100]]
Saving the new balance
Now that we have the new balance, let's overwrite the current balance with the new one:
$setUserVar[money;$bal]
At this point, the command will successfully increase the balance, but we want to ensure that users can only collect the reward once.
Setting a variable
In order to prevent users from collecting multiple rewards, we need to save information about whether each user has already used the command.
$setUserVar[hasCollected;true]
Adding a condition
Now that we have a variable called hasCollected
that returns whether the user has already collected the reward, let's use it to prevent users from collecting the same reward multiple times by putting this code at the beginning of the command:
$onlyIf[$getUserVar[hasCollected]!=true;You cannot collect the same reward more than once!]
Final result
Let's see if it works correctly
Note
You may not always limit executions to one per user, you can use other vars like server vars
to restrict the command to one execution per server.
🎉 Congrats!
You've learned how to make a command that can be used only one time!