fivem commands lua example

RegisterCommand("heal", function(source, args, rawCommand)
    local playerPed = GetPlayerPed(-1)
    if playerPed ~= nil then
        SetEntityHealth(playerPed, 200)
        notify("You have been healed.")
    end
end, false)

function notify(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(true, false)
end

Explanation:

  1. RegisterCommand("heal", function(source, args, rawCommand) ... end, false): Registers a new command named "heal". The function provided as the second argument will be executed when the command is triggered. The third argument false specifies that the command should not be restricted to admins only.

  2. local playerPed = GetPlayerPed(-1): Retrieves the player's Ped (short for pedestrian) entity. -1 represents the player running the command.

  3. if playerPed ~= nil then ... end: Checks if the player's Ped exists.

  4. SetEntityHealth(playerPed, 200): Sets the health of the player's Ped to 200, effectively healing them to full health (as 200 is the maximum health value in GTA V).

  5. notify("You have been healed."): Calls the notify function with the message "You have been healed." This function displays a notification on the screen with the provided text.

  6. function notify(text) ... end: Defines the notify function which takes a text parameter.

  7. SetNotificationTextEntry("STRING"): Sets the type of notification entry to a string.

  8. AddTextComponentString(text): Adds the provided text to the notification.

  9. DrawNotification(true, false): Draws the notification on the screen. The first parameter true indicates flashing, and the second parameter false is related to sound (which is disabled in this case).