Jump to content

serverside ped damage


Burak5312

Recommended Posts

hello how can i detect when serverside ped damaged by player

my purpose is ped turn face player and run if player not inside range

function findRotation( x1, y1, x2, y2 ) 
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) )
    return t < 0 and t + 360 or t
end


function createTank( x, y, z )
	if not (x) or not (y) or not (z) then return false end
    local THE_TANK = createPed( 0, x, y, z )
    local tankX, tankY, tankZ = getElementPosition( THE_TANK )
    local DETECT_RANGE = createColSphere( tankX, tankY, tankZ, 50 )
    local BLIP = createBlipAttachedTo ( THE_TANK, 23 )
    setBlipVisibleDistance( BLIP, 130 )
    attachElements( DETECT_RANGE, THE_TANK )

    local DETECT_TIMER = nil
    local TARGET_PLAYER = nil
    local FINDWAY_TIMER = nil

    if ( TARGET_PLAYER == nil ) then
       setTimer(function() setPedAnimation( THE_TANK, "ped", "idle_hbhb", -1, true, false, false, false ) end, 50, 1)
    end

local function findWay()
    local tankX, tankY,_ = getElementPosition( THE_TANK )
    local playerX, playerY,_ = getElementPosition( TARGET_PLAYER )
    setElementRotation( THE_TANK, 0, 0, findRotation(tankX, tankY, playerX, playerY), "default", true )
end

local function checkAround()
    if ( FINDWAY_TIMER == nil ) and ( TARGET_PLAYER == nil ) then
        local players = getElementsByType("player")
        for key, player in ipairs(players) do
           if( isElementWithinColShape(player, DETECT_RANGE ) ) then
              TARGET_PLAYER = player
              FINDWAY_TIMER = setTimer( findWay, 200, 0 )
              return true
           end
        end
    end
end
DETECT_TIMER = setTimer(checkAround, 2000, 0)

  -- HERE!!!
addEventHandler("onPedDamage", THE_TANK,
    function( attacker, attackerweapon, bodypart, loss )
       if( attacker ) and ( getElementType(attacker) == "player" ) and ( TARGET_PLAYER == nil ) and ( FINDWAY_TIMER ==nil ) then
          TARGET_PLAYER = attacker
          FINDWAY_TIMER = setTimer(findWay, 200, 0)
       end
    end
    )
end

createTank( 1942.90063, -1706.56201, 13.38281 )

 

Edited by Burak5312
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...