Jump to content

Problem about following a player


jingzhi

Recommended Posts

Posted

Clientscript

function cuffed()    
    local copx, copy, copz = getElementPosition (arrester) 
    local x, y, z = getElementPosition (localPlayer) 
    local copangle = ( 360 - math.deg ( math.atan2 ( ( copx - x ), ( copy - y ) ) ) ) % 360 
    setElementRotation(localPlayer,_,_,copangle) 
    local dist = getDistanceBetweenPoints2D (copx, copy, x, y) 
    if dist < 2 then 
        setControlState("forwards", false) 
        setControlState("sprint", false ) 
    else 
        setControlState("forwards", true) 
        setControlState("sprint", true ) 
    end 
end 
  
  
addEvent("onPlayerArrested",true) 
addEventHandler("onPlayerArrested",root, 
function(arrester) 
    arrester = arrester 
    setCameraTarget(arrester) 
    addEventHandler("onClientPreRender",root,cuffed) 
end) 
  
  
  
addEvent("onPlayerJailed",true) 
addEventHandler("onPlayerJailed",root, 
function() 
    removeEventHandler("onClientPreRender",root,cuffed) 
end) 
  
  

Hey guys, I am having this problem, which I am trying to make the criminal follow the police, but they dont work, nothing happens, only the camera is following the police, nothing in debugscript, and its triggered, please help! TY

Posted

Try using this:

function cuffed()   
    local copx, copy, copz = getElementPosition (arrester) 
    local x, y, z = getElementPosition (localPlayer) 
    local copangle = ( 360 - math.deg ( math.atan2 ( ( copx - x ), ( copy - y ) ) ) ) % 360 
     
    local rx, ry, _ = getElementRotation ( localPlayer ); 
     
    setElementRotation(localPlayer, rx, ry, copangle) 
    local dist = getDistanceBetweenPoints2D (copx, copy, x, y) 
    if dist < 2 then 
        setControlState("forwards", false) 
        setControlState("sprint", false ) 
    else 
        setControlState("forwards", true) 
        setControlState("sprint", true ) 
    end 
end 

_ isn't defined by MTA or Lua, so this could be causing the problem.

Posted
Try using this:
function cuffed()   
    local copx, copy, copz = getElementPosition (arrester) 
    local x, y, z = getElementPosition (localPlayer) 
    local copangle = ( 360 - math.deg ( math.atan2 ( ( copx - x ), ( copy - y ) ) ) ) % 360 
     
    local rx, ry, _ = getElementRotation ( localPlayer ); 
     
    setElementRotation(localPlayer, rx, ry, copangle) 
    local dist = getDistanceBetweenPoints2D (copx, copy, x, y) 
    if dist < 2 then 
        setControlState("forwards", false) 
        setControlState("sprint", false ) 
    else 
        setControlState("forwards", true) 
        setControlState("sprint", true ) 
    end 
end 

_ isn't defined by MTA or Lua, so this could be causing the problem.

Thank you very much, i will try out when i have a chance :)

Posted
Try using this:
function cuffed()   
    local copx, copy, copz = getElementPosition (arrester) 
    local x, y, z = getElementPosition (localPlayer) 
    local copangle = ( 360 - math.deg ( math.atan2 ( ( copx - x ), ( copy - y ) ) ) ) % 360 
     
    local rx, ry, _ = getElementRotation ( localPlayer ); 
     
    setElementRotation(localPlayer, rx, ry, copangle) 
    local dist = getDistanceBetweenPoints2D (copx, copy, x, y) 
    if dist < 2 then 
        setControlState("forwards", false) 
        setControlState("sprint", false ) 
    else 
        setControlState("forwards", true) 
        setControlState("sprint", true ) 
    end 
end 

_ isn't defined by MTA or Lua, so this could be causing the problem.

Here is the new script

addEvent("onPlayerArrested",true) 
addEventHandler("onPlayerArrested",root, 
function(arrester) 
    arrester = arrester 
    setCameraTarget(arrester) 
    addEventHandler("onClientPreRender",root,cuffed) 
end) 
  
function cuffed(arrester)   
    local copx, copy, copz = getElementPosition (arrester) 
    local x, y, z = getElementPosition (localPlayer) 
    local copangle = ( 360 - math.deg ( math.atan2 ( ( copx - x ), ( copy - y ) ) ) ) % 360 
    local rx, ry, _ = getElementRotation ( localPlayer ) 
    setElementRotation(localPlayer, rx, ry, copangle) 
    local dist = getDistanceBetweenPoints2D (copx, copy, x, y) 
    if dist < 2 then 
        setControlState("forwards", false) 
        setControlState("sprint", false ) 
    else 
        setControlState("forwards", true) 
        setControlState("sprint", true ) 
    end 
end 
  
  
  
  
  
  
addEvent("onPlayerJailed",true) 
addEventHandler("onPlayerJailed",root, 
function() 
    removeEventHandler("onClientPreRender",root,cuffed) 
end) 
  
  

Unfortunately, its still not working :|

Posted

Rename arrester parameter in onPlayerArrested function so it doesn't override the global one. Also, remove it from cuffed.

Posted
Rename arrester parameter in onPlayerArrested function so it doesn't override the global one. Also, remove it from cuffed.

Hey JR10, thank you very much, the problem is fix. But there is a little bug, that if the cop is at the east or west of the arrested criminal, the criminal runs to the opposite direction, can you please help to fix? Thank you !

addEvent("onPlayerArrested",true) 
addEventHandler("onPlayerArrested",root, 
function(arrestercop) 
    arrester = arrestercop 
    setCameraTarget(arrester) 
    toggleAllControls(localPlayer,true,true,false) 
    addEventHandler("onClientRender",root,cuffed) 
end) 
  
function cuffed()   
     
    local copx, copy, copz = getElementPosition (arrester) 
    local x, y, z = getElementPosition (localPlayer) 
    local copangle = ( 360 - math.deg ( math.atan2 ( ( copx - x ), ( copy - y ) ) ) ) % 360 
    local rx, ry, _ = getElementRotation (localPlayer) 
    setElementRotation(localPlayer,_,_, copangle) 
    local dist = getDistanceBetweenPoints2D (copx, copy, x, y) 
    if dist < 2 then 
        setControlState("forwards", false) 
        setControlState("sprint", false ) 
    else 
        setControlState("forwards", true) 
        setControlState("sprint", true ) 
    end 
end 
  
  
addEvent("onPlayerJailed",true) 
addEventHandler("onPlayerJailed",root, 
function() 
    removeEventHandler("onClientRender",root,cuffed) 
end) 
  
  

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...