GetPlayerFromNick: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 14: Line 14:


==Example==
==Example==
This example finds a [[player]] called ''Someguy'', and if he is found, he is killed and a message is displayed.
This example finds a [[player]] as specified in the command and outputs the direction and distance he is from the player who entered the command. For example: 'locate someguy'.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Find the player with the nickname 'Someguy'
function locatePlayer( player, command, who )
myPlayer = getPlayerFromNick ( "Someguy" )
local thePlayer = getPlayerFromNick(who) -- find the player that was specified in the command
-- If a player was returned (false is returned if the player isn't found)
if (thePlayer ~= false) then -- if a player was found
if ( myPlayer ~= false )
local x,y,z = getElementPosition(player) -- save the position of the player who entered the command
-- If the player was killed succesfully then
local xp,yp,zp = getElementPosition(thePlayer) -- save the position of the player who should be located
if ( killPlayer ( myPlayer ) ) then
local dir = nil
-- Say so in the chat
if (yp > y) then
outputChatBox ( "Someguy was found, and eliminated" )
dir = "N"
else
dir = "S"
end
if (xp > x) then
dir = dir.."E"
else
dir = dir.."W"
end
local distance = math.ceil(getDistanceBetweenPoints3D(x,y,z,xp,yp,zp))
outputChatBox(who.." found "..dir.." ("..distance..")",player) -- output the message
end
end
end
end
addCommandHandler("locate",locatePlayer)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Player functions}}
{{Player functions}}

Revision as of 22:19, 29 July 2007

This function returns a player element for the player with the name passed to the function.

Syntax

player getPlayerFromNick ( string playerName )

Required Arguments

  • playerName: A string containing the name of the player you want to reference

Returns

Returns a player element for the player with the nickname provided. If there is no player with that name, false is returned.

Example

This example finds a player as specified in the command and outputs the direction and distance he is from the player who entered the command. For example: 'locate someguy'.

function locatePlayer( player, command, who )
	local thePlayer = getPlayerFromNick(who) -- find the player that was specified in the command
	if (thePlayer ~= false) then -- if a player was found
		local x,y,z = getElementPosition(player) -- save the position of the player who entered the command
		local xp,yp,zp = getElementPosition(thePlayer) -- save the position of the player who should be located
		local dir = nil
		if (yp > y) then
			dir = "N"
		else
			dir = "S"
		end
		if (xp > x) then
			dir = dir.."E"
		else
			dir = dir.."W"
		end
		local distance = math.ceil(getDistanceBetweenPoints3D(x,y,z,xp,yp,zp))
		outputChatBox(who.." found "..dir.." ("..distance..")",player) -- output the message
	end
end
addCommandHandler("locate",locatePlayer)

See Also