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:
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 argumentfalse
specifies that the command should not be restricted to admins only.local playerPed = GetPlayerPed(-1)
: Retrieves the player's Ped (short for pedestrian) entity.-1
represents the player running the command.if playerPed ~= nil then ... end
: Checks if the player's Ped exists.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).notify("You have been healed.")
: Calls thenotify
function with the message "You have been healed." This function displays a notification on the screen with the provided text.function notify(text) ... end
: Defines thenotify
function which takes a text parameter.SetNotificationTextEntry("STRING")
: Sets the type of notification entry to a string.AddTextComponentString(text)
: Adds the provided text to the notification.DrawNotification(true, false)
: Draws the notification on the screen. The first parametertrue
indicates flashing, and the second parameterfalse
is related to sound (which is disabled in this case).