Getting Started with Commands
Intro
Commands are simple and easy to use via your server chat to do many things. Today we will show you how simple is it to create easy commands that can be useful for your server. Let's see the example below.
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => { let weaponHash = mp.joaat(weapon); player.giveWeapon(weaponHash, parseInt(ammo) || 10000); });
This example shows a command with name weapon which gives the player weapon with ammo. If the player doesn't specify the ammo, the system will give him 10000 ammo automatically.
How does it work?
Simply you go to the chat and type /weapon <weapon_name> <ammo>
For example, /weapon weapon_pistol 100
. This example gives me a pistol with 100 ammo.
to see the list of weapons Click Here.
Parameters
We have showed you an example up there about how the command system works. Now we'll tell you what is the use of the parameters that was shown.
mp.events.addCommand("<name>", (player, args, ...arg)
- <name>: This is the place where you can put your command name. For example i'll name it hello.
So now it will look like that:
mp.events.addCommand("hello", (player, fullText, ...arg)
- player: Player is a main definition you have to keep it the same and never change it. Player is used to get information about the player who executed the command.
- fullText: This will get all the words that the player typed after the command. For example if I typed
/Hello I am so cool
it will return you withI am so cool
. You can name it whatever you want, so i'll keep it FullText.
- ...arg: Those are optional parameters where you can get certain words. For example I made
/Hello I am so cool
, so if you tried to console.log(args1) it will return I. You can add any defined words you want.
So as a result the command will look like this:
mp.events.addCommand("hello", (player, fullText, arg1, arg2) => { player.outputChatBox(`Hello! ${player.name}`); });
So today we learned how the commands work and how you can use the parameters. For more detailed info about commands you can Click Here.