Skip to content

getElementHealth

Type: Shared

Pair: setElementHealth

This function returns the current health for the specified element. This can be a player, ped, vehicle, or object.

This example outputs the player and vehicle health (if player is in a vehicle) to chatbox using /health command:

getElementHealth-1.lua
function getMyHealth()
-- output player ped health to chat
outputChatBox("My health is: "..getElementHealth(localPlayer));
-- check if we are in a vehicle
local uVehicle = getPedOccupiedVehicle(localPlayer);
-- if valid vehicle, output its health to chat
if(uVehicle) then
outputChatBox("My vehicle health is: "..getElementHealth(uVehicle));
end
end
addCommandHandler("health", getMyHealth);

This example heals the player to 100 HP using /healme command if he's at 50 HP or lower:

getElementHealth-2.lua
function healMePlease()
-- heal the player if health is 50 or lower
if(getElementHealth(localPlayer) <= 50) then
setElementHealth(localPlayer, 100);
outputChatBox("You got healed!");
end
end
addCommandHandler("healme", healMePlease);