Events::addCommand: Difference between revisions
MrPancakers (talk | contribs) m (Cleaned up, made it more readable and understandable) |
m (Replaced HTML with template) |
||
| Line 17: | Line 17: | ||
==Example== | ==Example== | ||
This example gives a weapon to the current player with the specified number of ammo. If not specified, it will give 10000. | This example gives a weapon to the current player with the specified number of ammo. If not specified, it will give 10000. | ||
{{ServersideCode| | |||
<pre> | <pre> | ||
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => { | mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => { | ||
| Line 27: | Line 26: | ||
}); | }); | ||
</pre> | </pre> | ||
}} | |||
This example implements /me command | This example implements /me command | ||
{{ServersideCode| | |||
<pre> | <pre> | ||
mp.events.addCommand("me", (player, message) => { | mp.events.addCommand("me", (player, message) => { | ||
| Line 37: | Line 35: | ||
}); | }); | ||
</pre> | </pre> | ||
}} | |||
==See Also== | ==See Also== | ||
{{Event_functions}} | {{Event_functions}} | ||
Revision as of 11:46, 26 October 2018
This function registers a command handler.
Syntax
mp.events.addCommand(commandName, handlerFunction);
Required Arguments
- commandName: The name of the command you wish to attach a handler to
- handlerFunction: The function that you want the command to trigger, which has to be defined before you add the handler
Handler function parameters
player, fullText [, arg1, arg2, ...]
- player: The player object.
- fullText: All arguments after the command name.
- arg1, arg2, ...: Each argument after the command name.
Example
This example gives a weapon to the current player with the specified number of ammo. If not specified, it will give 10000.
Server-Side
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => {
var weaponHash = mp.joaat(weapon);
player.giveWeapon(weaponHash, parseInt(ammo) || 10000);
});
This example implements /me command
Server-Side
mp.events.addCommand("me", (player, message) => {
mp.players.broadcast(`* ${player.name}: ${message}`);
});
See Also
- Functions
- Properties