OnPlayerStealthKill: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Improve example.)
 
(One intermediate revision by the same user not shown)
Line 22: Line 22:
function onStealthKill(targetPlayer)
function onStealthKill(targetPlayer)
     outputChatBox("Stealth kill!", source) -- Tell the player he/she has done a stealth kill.
     outputChatBox("Stealth kill!", source) -- Tell the player he/she has done a stealth kill.
     outputChatBox("" .. getPlayerName(targetPlayer) .. " has been stealth-killed by " .. getPlayerName(source) .. ".")
     outputChatBox(getPlayerName(targetPlayer).." has been stealth-killed by "..getPlayerName(source)..".")
end
end
addEventHandler("onPlayerStealthKill", getRootElement(), onStealthKill) -- Adds a handler for the stealth kill event.
addEventHandler("onPlayerStealthKill", root, onStealthKill) -- Adds a handler for the stealth kill event.
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
Line 35: Line 34:
     cancelEvent(true, "No more stealth kills.") -- Aborts the stealth-kill.
     cancelEvent(true, "No more stealth kills.") -- Aborts the stealth-kill.
end
end
addEventHandler("onPlayerStealthKill", getRootElement(), onStealthKill) -- Adds a handler for the stealth kill event.
addEventHandler("onPlayerStealthKill", root, onStealthKill) -- Adds a handler for the stealth kill event.
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 43: Line 42:
Note: you won't get this to work properly without timer and 'fake' animation. Lots of scripters have broken their backs over this.
Note: you won't get this to work properly without timer and 'fake' animation. Lots of scripters have broken their backs over this.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler( "onPlayerStealthKill", getRootElement(),
local function preventStealthAnimation(thePlayer)
    function(targetPlayer)
    setPedAnimation(thePlayer, "ped", "0", -1, true, true, true, true, 350)
            setTimer(
end
                function(thePlayer)
 
                    setPedAnimation(thePlayer, "ped", "0", -1, true, true, true, true, 350)
function onPlayerStealthKill(targetPlayer)
                end, 50, 1, source
    setTimer(preventStealthAnimation, 50, 1, source)
            )
end
    end
addEventHandler("onPlayerStealthKill", root, onPlayerStealthKill)
)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}

Latest revision as of 08:14, 16 September 2021

This event is triggered when a player stealth kills another player.

Parameters

element targetPlayer
  • targetPlayer: the player or ped that is being stealth killed.

Source

The source of this event is the player that initiated the stealth kill.

Cancel effect

If this event is canceled, then the stealth kill is aborted.

Example

Click to collapse [-]
Server
function onStealthKill(targetPlayer)
     outputChatBox("Stealth kill!", source) -- Tell the player he/she has done a stealth kill.
     outputChatBox(getPlayerName(targetPlayer).." has been stealth-killed by "..getPlayerName(source)..".")
end
addEventHandler("onPlayerStealthKill", root, onStealthKill) -- Adds a handler for the stealth kill event.
Click to collapse [-]
Server
function onStealthKill(targetPlayer)
     cancelEvent(true, "No more stealth kills.") -- Aborts the stealth-kill.
end
addEventHandler("onPlayerStealthKill", root, onStealthKill) -- Adds a handler for the stealth kill event.
Click to collapse [-]
Server

This example will prevent the stealthkill animation of the attacker as it occurs, something which is tricky to achieve (hence this example is extra useful). Note: you won't get this to work properly without timer and 'fake' animation. Lots of scripters have broken their backs over this.

local function preventStealthAnimation(thePlayer)
    setPedAnimation(thePlayer, "ped", "0", -1, true, true, true, true, 350)
end

function onPlayerStealthKill(targetPlayer)
    setTimer(preventStealthAnimation, 50, 1, source)
end
addEventHandler("onPlayerStealthKill", root, onPlayerStealthKill)

See Also

Player events


Event functions