GetElementHealth: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 21: Line 21:
function showLocalHealth()
function showLocalHealth()
-- get the player's health and output it
-- get the player's health and output it
local playerHealth = getElementHealth ( getLocalPlayer() )
local playerHealth = getElementHealth ( localPlayer )
outputChatBox ( "Your health: " .. playerHealth )
outputChatBox ( "Your health: " .. playerHealth )


-- get the player's vehicle: if he is in one, output its health as well
-- get the player's vehicle: if he is in one, output its health as well
local playerVehicle = getPedOccupiedVehicle ( getLocalPlayer() )
local playerVehicle = getPedOccupiedVehicle ( localPlayer )
if playerVehicle then
if playerVehicle then
local vehicleHealth = getElementHealth ( playerVehicle ) / 10  -- Divide this by 10, as default the denominator is 1000
local vehicleHealth = getElementHealth ( playerVehicle ) / 10  -- Divide this by 10, as default the denominator is 1000

Revision as of 16:10, 19 August 2015

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

Syntax

float getElementHealth ( element theElement )

OOP Syntax Help! I don't understand this!

Method: element:getHealth(...)
Variable: .health


Required Arguments

  • theElement: The player or vehicle whose health you want to check.

Returns

Returns a float indicating the element's health, or false if invalid arguments were passed.

Example

Click to collapse [-]
Clientside example

This example outputs the health of the player who enters the command 'showhealth', and their vehicle's health.

function showLocalHealth()
	-- get the player's health and output it
	local playerHealth = getElementHealth ( localPlayer )
	outputChatBox ( "Your health: " .. playerHealth )

	-- get the player's vehicle: if he is in one, output its health as well
	local playerVehicle = getPedOccupiedVehicle ( localPlayer )
	if playerVehicle then
		local vehicleHealth = getElementHealth ( playerVehicle ) / 10  -- Divide this by 10, as default the denominator is 1000
		outputChatBox ( "Your vehicle's health: " .. vehicleHealth )
	end
end
addCommandHandler ( "showhealth", showLocalHealth )

See Also