setElementHealth | Multi Theft Auto: Wiki Skip to content

setElementHealth

Client-side
Server-side
Shared

Pair: getElementHealth

This function sets the health of a player, ped, vehicle, or object element.

In the case of the vehicle element, the health ranges from 0 to 1000.

  • 1000: no damage at all
  • 650: white steam 0%, black smoke 0%
  • 450: white steam 100%, black smoke 50%
  • 250: white steam 0%, black smoke 100%
  • 249: fire with big black smoke (blowing up)

OOP Syntax Help! I don't understand this!

  • Method: element:setHealth(...)
  • Variable: .health

Syntax

bool setElementHealth ( element theElement, float newHealth )
Required Arguments
  • theElement: The player, ped, vehicle, or object element whose health you want to set.
  • newHealth: A float indicating the new health to set for the element.

Returns

  • bool: result

Returns true if the new health was set successfully, false otherwise.

Code Examples

This example heals the player vehicle using the command /repairvehicle if it's below 1000 HP:

function repairMyVehicle()
-- check if we are in a vehicle
local uVehicle = getPedOccupiedVehicle(localPlayer);
if(uVehicle) then
-- does our vehicle need repair? remember, vehicle health ranges from 0 to 1000
if(getElementHealth(uVehicle) < 1000) then
-- note: setElementHealth does not repair the visual aspect of vehicles, use fixVehicle instead!
setElementHealth(uVehicle, 1000);
else
outputChatBox("Your vehicle is already at full health!", 255, 0, 0);
end
else
outputChatBox("You are not in a vehicle!", 255, 0, 0);
end
end
addCommandHandler("repairvehicle", repairMyVehicle);

This example changes the player health to new specified value using /sethealth command:

function setMyHealth(uPlayer, strCommand, fAmount)
-- safety check if we got a valid new health value
local fAmount = tonumber(fAmount);
if(not fAmount) then
return outputChatBox("Invalid health value entered!", uPlayer, 255, 0, 0);
end
-- change the player's health
setElementHealth(uPlayer, fAmount);
outputChatBox("Your health was set to "..fAmount.." HP!", uPlayer, 0, 255, 0);
end
addCommandHandler("sethealth", setMyHealth)