Jump to content

onClientPlayerTarget and onPlayerTarget


Ryder!

Recommended Posts

hi,

why i can only attach the "onClientPlayerTarget" and "onPlayerTarget" (client side and server side) to the root element (getRootElement() ) it doesn't work if i attach those event to a ped for example.

  
function onTarget(element) 
     setPedAnimation( element, "ped", "handsup" ) 
end 
  
addEventHandler( "onClientPlayerTarget", getElementByID( "testPed" ), onTarget ) 
  

Map File:

  
<ped id="testPed" posX="2089.5925292969" posY="2213.3474121094" posZ="10.8203125" model="0" /> 
  

Thanks bye

Link to comment

If the way you prefer doesn't work, you just need to find a workaround. ;)

function onTarget(element) 
     if element==getElementByID("testped") then --If getElementByID("testped") is targeted by the source then... 
          setPedAnimation( element, "ped", "handsup" ) -- Raise his hands! 
     end 
end 
  
addEventHandler( "onClientPlayerTarget", getRootElement(), onTarget ) -- getRootElement() should make it do the same if someone else targets the ped. If you only want it to happen with the local player, replace it with getLocalPlayer() 

Untested, but should work. Note that you can also replace...

     if element==getElementByID("testped") then 

...With...

     if getElementType(element)=="ped" then 

... To make it work with all peds you have. This might save you quite some time if you want it to work on all of your peds. ;)

Link to comment

Thanks for your answer,

The reason why i want to attach the event to some particular peds it's because i need to retrieve when the player is not targetting the ped anymore.

This is what i want to do:

  
function onTarget( element ) 
     if not element then -- im sure that the player is not targetting the ped anymore because the event is attached to a particular ped. 
          --my code 
     end 
end 
  
addEventHandler( "onClientPlayerTarget", getElementByID("testPed"), onTarget ) 
  

I can't do the same by attaching the event to the root element because "my code" will be executed when i'm not targetting any element anymore.

Link to comment

Ok, I'll try to help you on your way.

-- Code from before, note that this should work. Especially because I still didn't hear that it didn't from you. 
function onTarget(element) 
     if element==getElementByID("testped") then 
          setPedAnimation( element, "ped", "handsup" ) 
          setTimer(checkTargeted,500,1,source,element) -- We want the ped to drop his hands if he's not being targeted anymore, in a simple way. For that reason, we're executing checkTargeted. This is located between line 11 and line 17. 
     end 
end 
  
addEventHandler( "onClientPlayerTarget", getRootElement(), onTarget ) 
  
function checkTargeted(thePlayer,element) -- source is now renamed thePlayer, because I entered the argument name different here 
     if getPedTarget(thePlayer)~=element then -- Is the ped still being targeted? (note: ~= means "isn't equal to" in Lua) 
          setPedAnimation(element) -- No. Therefor, lower his hands 
     else 
          setTimer(checkTargeted,500,1,thePlayer,element) -- Yes. Therefor, check back later 
     end 
end 

Note that this is a very simple way, but it should be synched. So when someone else targets the ped, you should see the animation too. :) Note though that the script is (still) client-side, so don't forget to add type="client" in the meta.xml! ;)

For more info on the functions I used:

getPedTarget

setTimer

setPedAnimation

EDIT: Forgot about targeting for xx seconds, but you should be able to make it with the functions given. For instance setTimer. However, if you want to use setTimer, you might also want to use killTimer. Try to make something with it, it doesn't matter if it fails, just post it up and we'll see what went wrong. ;)

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