GivePlayerMoney: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (added remarks regarding the amount parameter)
 
(28 intermediate revisions by 17 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
Adds money to the player's current money amount.
{{Server client function}}
This function adds money to a [[player]]'s current money amount. To set absolute values, [[setPlayerMoney]] can be used.<br>
{{Note|Using this function client side (not recommended) will not change a players money server side.}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">givePlayerMoney ( player, money )</syntaxhighlight>  
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">bool givePlayerMoney ( player thePlayer, int amount )</syntaxhighlight>
{{OOP||[[player]]:giveMoney|money}}
===Required Arguments===
*'''thePlayer:''' the [[player]] you are giving the money to.
*'''amount:''' a positive integer number specifying the amount of money to give to the player.
</section>
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">bool givePlayerMoney ( int amount )</syntaxhighlight>
{{OOP||[[Player]].giveMoney}}
===Required Arguments===
*'''amount:''' a positive integer number specifying the amount of money to give to the player.
</section>


===Required Arguments===  
===Returns===
*'''Player:''' Tells the function to give money to a player
Returns ''true'' if the money was added, or ''false'' if invalid parameters were passed.
*'''Money:''' A whole integer specifying the amount of money to give to the player
 
==Remarks==
As of MTA SA version 1.5.9, despite the documentation claiming that '''amount''' should be a positive integer, this function does work with negative values or zero. In that case the function does still add the number to the game money value, in the negative case resulting in a decreased value.


==Example==   
==Example==   
This example gives a player money when he types "givecash" in console
<section show="true" name="Example 1 - Client and Server" class="server">
<syntaxhighlight lang="lua">addCommandHandler ( "givecash", "giveCash" ) --add the command "givecash"
This example gives a player money when using "givecash" command.
function giveCash ( player, command, amount ) --when the givecash command is called
<syntaxhighlight lang="lua">
givePlayerMoney ( player, amount ) --gives the player money according to the amount
function consoleGiveCash ( thePlayer, command, amount ) --when the givecash command is called
end</syntaxhighlight>
givePlayerMoney ( thePlayer, amount ) --give the player money according to the amount
end
addCommandHandler ( "givecash", consoleGiveCash  ) --add a handler function for the command "givecash"
</syntaxhighlight>
</section>
 
<section show="true" name="Example 2 - Server" class="server">
This example gives a player one thousand dollars, as a reward for killing another player.
<syntaxhighlight lang="lua">
function rewardOnWasted ( ammo, killer, killerweapon, bodypart )
--if there is a killer, and that killer is not the same person as whoever died
if ( killer ) and ( killer ~= source ) then
givePlayerMoney ( killer, 1000 ) --reward the killer with 1000 cash.
end
end
addEventHandler ( "onPlayerWasted", root, rewardOnWasted ) --attach the rewardOnWasted function to the relevant event.
</syntaxhighlight>
</section>
<section show="true" name="Example 3 - Server" class="server">
This example Creates money Money (dollar symbol) pickup and gives 30,000 dollars on Pick up hit.
<syntaxhighlight lang="lua">
local money = createPickup (1896.4000244141, -1950.9000244141, 13, 3, 1274, 10000 )
function pickupUse ( player )
    givePlayerMoney ( player, 30000 )
end
addEventHandler ( "onPickupUse", money, pickupUse )</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Player functions}}
{{Player functions}}
[[pt-br:GivePlayerMoney]]
[[ru:GivePlayerMoney]]

Latest revision as of 05:45, 7 October 2021

This function adds money to a player's current money amount. To set absolute values, setPlayerMoney can be used.

[[{{{image}}}|link=|]] Note: Using this function client side (not recommended) will not change a players money server side.

Syntax

Click to collapse [-]
Server
bool givePlayerMoney ( player thePlayer, int amount )

OOP Syntax Help! I don't understand this!

Method: player:giveMoney(...)
Variable: .money


Required Arguments

  • thePlayer: the player you are giving the money to.
  • amount: a positive integer number specifying the amount of money to give to the player.
Click to collapse [-]
Client
bool givePlayerMoney ( int amount )

OOP Syntax Help! I don't understand this!

Method: Player.giveMoney(...)


Required Arguments

  • amount: a positive integer number specifying the amount of money to give to the player.

Returns

Returns true if the money was added, or false if invalid parameters were passed.

Remarks

As of MTA SA version 1.5.9, despite the documentation claiming that amount should be a positive integer, this function does work with negative values or zero. In that case the function does still add the number to the game money value, in the negative case resulting in a decreased value.

Example

Click to collapse [-]
Example 1 - Client and Server

This example gives a player money when using "givecash" command.

function consoleGiveCash ( thePlayer, command, amount ) --when the givecash command is called
	givePlayerMoney ( thePlayer, amount ) --give the player money according to the amount
end
addCommandHandler ( "givecash", consoleGiveCash  ) --add a handler function for the command "givecash"
Click to collapse [-]
Example 2 - Server

This example gives a player one thousand dollars, as a reward for killing another player.

function rewardOnWasted ( ammo, killer, killerweapon, bodypart )
	--if there is a killer, and that killer is not the same person as whoever died
	if ( killer ) and ( killer ~= source ) then 
		givePlayerMoney ( killer, 1000 ) --reward the killer with 1000 cash.
	end
end
addEventHandler ( "onPlayerWasted", root, rewardOnWasted ) --attach the rewardOnWasted function to the relevant event.
Click to collapse [-]
Example 3 - Server

This example Creates money Money (dollar symbol) pickup and gives 30,000 dollars on Pick up hit.

local money = createPickup (1896.4000244141, -1950.9000244141, 13, 3, 1274, 10000 )
function pickupUse ( player )
    givePlayerMoney ( player, 30000 )
end
addEventHandler ( "onPickupUse", money, pickupUse )

See Also