Shared function
setElementHealth
This function sets the health of a player, ped, vehicle or object element.
Syntax
bool setElementHealth ( element theElement, float newHealth )
- element theElement : The player, ped, vehicle or object element whose health you want to set.
- float newHealth : A float indicating the new health to set for the element.
void ( )
void ( )
Returns
Returns true if the new health was set successfully, false otherwise.
- bool result
Issues
- mtasa-blue #3807 : hpbar on hud is not compatible visually with MAX_HEALTH stat
- mtasa-blue #3791 : setPedArmor and setElementHealth synchronization problems from Client to Server
- mtasa-blue #2223 : setElementHealth in onClientPlayerDamage bug
- mtasa-blue #1423 : When you setElementHealth under onClientPlayerDamage, the local (hit) player rotates automatically
- mtasa-blue #448 : Crash when plane explode
Examples
Example 1 (Client-side)
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);
Example 2 (Server-side)
This example changes the player health to new specified value using /sethealth
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)
