📜  Fivem registercommand (1)

📅  最后修改于: 2023-12-03 15:00:46.345000             🧑  作者: Mango

FiveM RegisterCommand

FiveM is a modification platform for Grand Theft Auto V, focused on providing a robust and customizable experience for both players and developers. One of the key features of FiveM is the ability to create custom commands, which can be registered using the registercommand function.

Syntax

The syntax for registercommand is as follows:

RegisterCommand(commandName, handler, restricted)
  • commandName (string): The name of the command, without the / prefix.
  • handler (function): The function to be executed when the command is called.
  • restricted (boolean, optional): Whether or not the command can only be executed by players with the command ace permission.
Example usage

Here is an example of how to create a simple /hello command that will display a greeting message to the player who executes it:

RegisterCommand("hello", function(source, args)
    TriggerClientEvent("chat:addMessage", source, { args = {"^1SERVER", "^0Hello, " .. GetPlayerName(source)} })
end, false)

This command uses the TriggerClientEvent function to send a chat message to the player who executes it, with the message content "Hello, [playername]!".

Restrictions

If you want to restrict the command to players with the command ace permission, you can add the third parameter, restricted, and set it to true:

RegisterCommand("kick", function(source, args)
    -- Check if player has permission to execute the command
    if not IsPlayerAceAllowed(source, "command") then
        TriggerClientEvent("chat:addMessage", source, { args = {"^1SERVER", "^0You do not have permission to execute this command!"} })
        return
    end

    -- Execute command code here
    ...
end, true)

This command will only be executable by players who have the command ace permission, and will display an error message to players who do not have the permission.

Conclusion

registercommand is a powerful feature of FiveM that allows developers to create custom commands for their servers. By using the handler function, you can execute any code you want when the command is called, making the possibilities endless. With the option to restrict commands to players with certain permissions, you can ensure that your server remains secure and stable.