SetPlayerHealth: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 16: Line 16:
==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example finds "player" and when "slap" is entered, their health is decreased
This example adds a console command that lets players "slap" each other (doing 20 damage).
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function slap ( player, command ) --calls the function
function slap ( sourcePlayer, command, targetPlayerName )
setPlayerHealth ( player, getPlayerHealth(player) - 20 ) --sets the player health
    local targetPlayer = getPlayerFromNick ( targetPlayerName )           -- look up the player to be slapped
end
    if targetPlayer then                                                  -- check if we found him
        setPlayerHealth ( thePlayer, getPlayerHealth(targetPlayer) - 20 ) -- subtract 20 from his health
    else                                                                  -- if the target player wasn't found, output an error message
        outputChatBox ( "There is no player named " .. targetPlayerName .. "!", sourcePlayer )
    end
end
addCommandHandler ( "slap", slap )
addCommandHandler ( "slap", slap )
</syntaxhighlight></section>
</syntaxhighlight></section>

Revision as of 11:13, 16 August 2007

This function allows you to set a player's health.

Syntax

bool setPlayerHealth ( player thePlayer, float playerHealth )

Required Arguments

  • thePlayer: The player whose health you want to set.
  • playerHealth: The value you want to set the health to. Valid values are from 0 (dead) to 100 (full health).

Returns

Returns true if the player's health was set succesfully. Returns false if the player argument is invalid, or the health argument is out of range.

Example

Click to collapse [-]
Server

This example adds a console command that lets players "slap" each other (doing 20 damage).

function slap ( sourcePlayer, command, targetPlayerName )
    local targetPlayer = getPlayerFromNick ( targetPlayerName )           -- look up the player to be slapped
    if targetPlayer then                                                  -- check if we found him
        setPlayerHealth ( thePlayer, getPlayerHealth(targetPlayer) - 20 ) -- subtract 20 from his health
    else                                                                  -- if the target player wasn't found, output an error message
        outputChatBox ( "There is no player named " .. targetPlayerName .. "!", sourcePlayer )
    end
end
addCommandHandler ( "slap", slap )

See Also