SetElementHealth: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: {{Server client function}} __NOTOC__ This function sets the health for the specified element. This can be a player or a vehicle. ==Syntax== <section name="Server and client" c...)
 
mNo edit summary
Line 32: Line 32:
     if targetPlayer then
     if targetPlayer then
         -- subtract 20 from his health
         -- subtract 20 from his health
         setElementHealth ( thePlayer, getElementHealth(targetPlayer) - 20 )
         setElementHealth ( targetPlayer, getElementHealth(targetPlayer) - 20 )
     else
     else
         -- otherwise, output an error message
         -- otherwise, output an error message

Revision as of 01:30, 17 September 2007

This function sets the health for the specified element. This can be a player or a vehicle.

Syntax

Click to collapse [-]
Server and client
bool setElementHealth ( element theElement, float newHealth )

Required Arguments

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

Returns

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

Example

Click to collapse [-]
Server

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 canPlayerUseFunction( sourcePlayer, command ) then
        outputChatBox ( "You cannot use this command.", sourcePlayer )
        return false
    end
    -- look up the player to be slapped
    local targetPlayer = getPlayerFromNick ( targetPlayerName )
    -- if there's a player with such name,
    if targetPlayer then
        -- subtract 20 from his health
        setElementHealth ( targetPlayer, getElementHealth(targetPlayer) - 20 )
    else
        -- otherwise, output an error message
        outputChatBox ( "There is no player named " .. targetPlayerName .. "!", sourcePlayer )
    end
end
-- add our function as a handler for "hpslap"
addCommandHandler ( "hpslap", hpSlap )

See Also