From Multi Theft Auto: Wiki
Jump to navigation Jump to search

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

[[{{{image}}}|link=|]] Note: In the case of the vehicle element, the following effects appear, depending on the health value:
  • 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


bool setElementHealth ( element theElement, float newHealth )

OOP Syntax Help! I don't understand this!

Method: element:setHealth(...)
Variable: .health
Counterpart: getElementHealth

Required Arguments

  • theElement: The ped, vehicle or object whose health you want to set.
  • newHealth: A float indicating the new health to set for the element.


Returns true if the new health was set successfully, or false if invalid arguments were passed.


Click to collapse [-]

This example adds a 'hpslap' console command that lets players "slap" others (doing 20 damage).

function hpSlap ( sourcePlayer, command, targetPlayerName )
    -- check if the user has access to it first
    if not hasObjectPermissionTo(sourcePlayer, "command.slap", false) then
        outputChatBox ( "You cannot use this command.", sourcePlayer )
        return false
    -- look up the player to be slapped
    local targetPlayer = getPlayerFromName ( targetPlayerName )
    -- if there's a player with such name,
    if targetPlayer then
        -- subtract 20 from his health
        setElementHealth ( targetPlayer, getElementHealth(targetPlayer) - 20 )
        -- otherwise, output an error message
        outputChatBox ( "There is no player named " .. targetPlayerName .. "!", sourcePlayer )
-- add our function as a handler for "hpslap"
addCommandHandler ( "hpslap", hpSlap )
Click to collapse [-]
Example 1

This example setting health by a command.

function Health(player, command, amount)
    setElementHealth(player, tonumber(amount))
addCommandHandler("set", Health)


Issue ID Description
#414 Using setElementHealth on a dead ped makes it invincible

See Also