SetElementVelocity: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Updated the page according to r6658)
Line 25: Line 25:


==Example==
==Example==
This example 'copies' the speed of a specific player (called ''someguy'' in this example) to another player (called ''Ted'' in this example).
<section class="server" name="Server" show=true>
This example adds a function which copies the speed of a random player to another random player. If there are less than 2 players it returns ''false''.


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local randomPlayer = getRandomPlayer()
function equalTwoRandomPlayersVelocity()
speedx, speedy, speedz = getElementVelocity (randomPlayer) -- get the velocity of the player named "someguy"
    if getPlayerCount() < 2 then -- If there's only one player (or no players) this doesn't make sense
setElementVelocity (randomPlayer , speedx, speedy, speedz )     -- copy the velocity to a player named "Ted"
        return false
    end
    local randomPlayer1, randomPlayer2 = getRandomPlayer(), getRandomPlayer() -- Get two random players
    while randomPlayer1 == randomPlayer2 do -- Make sure the two players are different
        randomPlayer2 = getRandomPlayer()
    end
    local speedx, speedy, speedz = getElementVelocity (randomPlayer1) -- Get the velocity of the first random player
    setElementVelocity(randomPlayer2, speedx, speedy, speedz) -- Copy that velocity to the second random player
    outputChatBox("Now " .. getPlayerName(randomPlayer2) .. " runs as fast as " .. getPlayerName(randomPlayer1) .. "!", root, 255, 128, 0)
    return true
end
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{element functions}}
{{element functions}}

Revision as of 12:05, 9 July 2014

This function sets the velocity (movement speeds) along each axis, for an element.

This is not compatible with all elements. Only the following elements are compatible:

Syntax

bool setElementVelocity ( element theElement, float speedX, float speedY, float speedZ )

Required Arguments

  • theElement: The element you wish to set the velocity of.
  • speedX: A floating point value determining the speed along the X axis.
  • speedY: A floating point value determining the speed along the Y axis.
  • speedZ: A floating point value determining the speed along the Z axis.

Returns

Returns true if the speed was set successfully, false if a bad element was specified or other bad arguments.

Example

Click to collapse [-]
Server

This example adds a function which copies the speed of a random player to another random player. If there are less than 2 players it returns false.

function equalTwoRandomPlayersVelocity()
    if getPlayerCount() < 2 then -- If there's only one player (or no players) this doesn't make sense
        return false
    end
    local randomPlayer1, randomPlayer2 = getRandomPlayer(), getRandomPlayer() -- Get two random players
    while randomPlayer1 == randomPlayer2 do -- Make sure the two players are different
        randomPlayer2 = getRandomPlayer()
    end
    local speedx, speedy, speedz = getElementVelocity (randomPlayer1) -- Get the velocity of the first random player
    setElementVelocity(randomPlayer2, speedx, speedy, speedz) -- Copy that velocity to the second random player
    outputChatBox("Now " .. getPlayerName(randomPlayer2) .. " runs as fast as " .. getPlayerName(randomPlayer1) .. "!", root, 255, 128, 0)
    return true
end

See Also