IsElement: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(Undo revision 50124 by Marcin778 (talk))
 
(9 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
This function checks if a value is an [[element]] or not.
This function checks if a value is an [[element]] or not.
 
{{Note|This function is not reliable as element ids are eventually recycled. Always make sure you nil variables containing an element after calling [[destroyElement]] or handle [[onElementDestroy]] for players and elements that might be destroyed by another resource}}
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">bool isElement ( var )</syntaxhighlight>
<syntaxhighlight lang="lua">bool isElement ( var theValue )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
* '''var''': A value that may or may not be an element.
* '''theValue''': The value that we want to check.


===Returns===
===Returns===
Line 12: Line 13:


==Example==
==Example==
This example checks if the
<section name="Server" class="server" show="true">
This serverside function kills a player when it's passed his name or his element.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--we create a variable
function killPlayer2 ( argument )
local var
-- if the argument is an element, and also a player,
--we randomise its value
if isElement( argument ) and getElementType( argument ) == "player" then
if math.random(0,1) == 1 then var = getRootElement()
-- kill him
else var = "string"
killPed ( argument )
end
 
--and then we can check if it's an element or not
-- if it isn't an element, but a string, it could be a name
if isElement(var) then outputChatBox("math.random generated number 1")
elseif type ( argument ) == "string" then
else then outputChatBox("math.random generated number 0")
-- retrieve the player with that name
local playerElement = getPlayerFromName( argument )
-- if a player with such a name exists,
if playerElement then
-- kill him
killPed ( playerElement )
end
end
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Element functions}}
{{Element functions}}

Latest revision as of 18:38, 1 January 2017

This function checks if a value is an element or not.

[[{{{image}}}|link=|]] Note: This function is not reliable as element ids are eventually recycled. Always make sure you nil variables containing an element after calling destroyElement or handle onElementDestroy for players and elements that might be destroyed by another resource

Syntax

bool isElement ( var theValue )

Required Arguments

  • theValue: The value that we want to check.

Returns

Returns true if the passed value is an element, false otherwise.

Example

Click to collapse [-]
Server

This serverside function kills a player when it's passed his name or his element.

function killPlayer2 ( argument )
	-- if the argument is an element, and also a player,
	if isElement( argument ) and getElementType( argument ) == "player" then
		-- kill him
		killPed ( argument )

	-- if it isn't an element, but a string, it could be a name
	elseif type ( argument ) == "string" then
		-- retrieve the player with that name
		local playerElement = getPlayerFromName( argument )
		-- if a player with such a name exists,
		if playerElement then
			-- kill him
			killPed ( playerElement )
		end
	end
end

See Also