IsPedDucked: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(Added new example)
Line 20: Line 20:
if isPedDucked ( randomPlayer ) then
if isPedDucked ( randomPlayer ) then
outputChatBox ( getPlayerName ( randomPlayer ) .. " is currently crouching." )
outputChatBox ( getPlayerName ( randomPlayer ) .. " is currently crouching." )
end
</syntaxhighlight>
This example creates a function that lets you toggle the crouching state of a ped.
<syntaxhighlight lang="lua">
function setPedDucked(ped, bool)
local alreadyDucked = isPedDucked(ped)
if (alreadyDucked and not bool) then
if (ped == localPlayer) then
setControlState("crouch", true)
setTimer(setControlState, 50, 1, "crouch", false)
else
setPedControlState(ped, "crouch", true)
setTimer(setPedControlState, 50, 1, ped, "crouch", false)
end
return true
elseif (not alreadyDucked and bool) then
if (ped == localPlayer) then
setControlState("crouch", true)
setTimer(setControlState, 50, 1, "crouch", false)
else
setPedControlState(ped, "crouch", true)
setTimer(setPedControlState, 50, 1, ped, "crouch", false)
end
return true
end
return false
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 17:17, 15 November 2014

This function checks if the specified ped is ducked (crouched) or not.

Syntax

bool isPedDucked ( ped thePed )

Required Arguments

  • thePed: The ped to check.

Returns

Returns true if the ped is ducked, false otherwise.

Example

Click to collapse [-]
Client

This example checks if a random player is ducked or not, and if so displays a message in the chat box.

local players = getElementsByType ( "player" )
local randomPlayer = players[math.random(#players)]
if isPedDucked ( randomPlayer ) then
	outputChatBox ( getPlayerName ( randomPlayer ) .. " is currently crouching." )
end

This example creates a function that lets you toggle the crouching state of a ped.

function setPedDucked(ped, bool)
	local alreadyDucked = isPedDucked(ped)
	if (alreadyDucked and not bool) then
		if (ped == localPlayer) then
			setControlState("crouch", true)
			setTimer(setControlState, 50, 1, "crouch", false)
		else
			setPedControlState(ped, "crouch", true)
			setTimer(setPedControlState, 50, 1, ped, "crouch", false)
		end
		return true
	elseif (not alreadyDucked and bool) then
		if (ped == localPlayer) then
			setControlState("crouch", true)
			setTimer(setControlState, 50, 1, "crouch", false)
		else
			setPedControlState(ped, "crouch", true)
			setTimer(setPedControlState, 50, 1, ped, "crouch", false)
		end
		return true
	end
	return false
end

See Also