- Thread Author
- #1
Creating Teams / Working with RAGE:MP Commands
To register a new command, we use the mp.events.addCommand function, which allows you to bind a handler function to the specified command.
JavaScript:
mp.events.addCommand(commandName, handlerFunction);
commandName - the name of the handlerFunction command
- the handler function that will be called when someone enters this command. The following arguments are passed to this function:
Code:
player, fullText [, arg1, arg2, ...]
player - игрок который ввел команду
fullText - массив всех аргументов введенных после команды
[, arg1, arg2, ...] - аргументы введенные после самой команды
Now, let's create some simple commands for example.
Example 1 - Command /me
JavaScript:
mp.events.addCommand("me", (player, message) => {
mp.players.broadcast(`* ${player.name}: ${message}`);
});
Example 2 - Command /weapon
The command gives the player the selected weapon with a specified number of rounds (if not specified, then 1000). For example, the /weapon weapon_revolver 500 will give 500 rounds of the Heavy Revolver pistol.
JavaScript:
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => {
var weaponHash = mp.joaat(weapon);
player.giveWeapon(weaponHash, parseInt(ammo) || 10000);
});
You can also track command input using the playerCommand event. It is triggered for commands that have not been registered with mp.events.addCommand.
Example 3 - Tracking Erroneous Commands
JavaScript:
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => {
var weaponHash = mp.joaat(weapon);
player.giveWeapon(weaponHash, parseInt(ammo) || 10000);
});
Example 3 - Tracking Erroneous Commands
JavaScript:
mp.events.add('playerCommand', (player, command) => {
player.outputChatBox(`${command} не правильная команда. Введите /help для помощи.`);
});
Example 4 - Command /hello
The command simply displays the phrase "Hello!" to the player in the chat
JavaScript:
mp.events.add("playerCommand", (player, command) => {
const args = command.split(/[ ]+/); // получаем список аргументов команды
const commandName = args.splice(0, 1)[0]; // получаем название команды
if (commandName === "hello") {
player.outputChatBox("Привет!");
}
});