Need help detecting is player is aiming at ped

I am trying to make it where if a player aims a gun at a ped certain things happen. This is what i have so far and not working. this is in LUA

anyone able to help?

ESX		  = nil

Citizen.CreateThread(function()
	while ESX == nil do
		TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
		Citizen.Wait(0)
	end
end)

Citizen.CreateThread(function() -- Creates thread
	while true do
		Citizen.Wait(1)
		local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId(-1))
		if aiming then
			print("in 1")
			if DoesEntityExist(targetPed) and IsEntityAPed(targetPed) then
                          print("in 2")
                          -- DO STUFF HERE
   
  			end -- end if DoesEntityExist
		end -- end if aiming
	end -- end while
end)

1 Like

When I check on http://www.dev-c.com/nativedb/ it says that GetEntityPlayerIsFreeAimingAt return a bool

I do have tested your script on my server, and it work pretty great, I do have the print(“in 2”) in my client console, so everything is working pretty fine :slight_smile:

interesting. so when you point a gun at a ped it says “2”?

When I point my gun on a ped yes it says “in 2” in my console, make sure that you are in range with your gun

ok maybe its my pc then. im now just getting a resource warning about it

ESX		  = nil

Citizen.CreateThread(function()
	while ESX == nil do
		TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
		Citizen.Wait(0)
	end
end)

Citizen.CreateThread(function() -- Creates thread
	while true do
		Citizen.Wait(1)
		local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId(-1))
		if aiming then
			print("in 1")
			if DoesEntityExist(targetPed) and IsEntityAPed(targetPed) then
                          print("in 2")
                        ESX.ShowNotification("~r~ Don't aim at me again bitch!")

  			end -- end if DoesEntityExist
		end -- end if aiming
	end -- end while
end)

You have a resource warning because of the both print in the console, you can try to use Citizen.Wait(100), but it work fine

Also make sure your resource is started too, and it really should work

ya it is now… now im trying to make that targeted ped play an animation using:

RequestAnimDict("anim@dictionary")
TaskPlayAnim(targetPed,"random@arrests", "idle_2_hands_up", 1.0, -1.0, 5000,0, 1, true, true, true)

am i doing that correct?

1 Like

Not really by that way you can use this code, its working pretty well

ESX		  = nil

Citizen.CreateThread(function()
	while ESX == nil do
		TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
		Citizen.Wait(0)
	end
end)

Citizen.CreateThread(function() -- Creates thread
	while true do
		Citizen.Wait(1)
		local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId(-1))
		if aiming then
			print("in 1")
			if DoesEntityExist(targetPed) and IsEntityAPed(targetPed) then
                          print("in 2")
                          surrender(targetPed)

  			end -- end if DoesEntityExist
		end -- end if aiming
	end -- end while
end)

function surrender(targetPed)
  RequestAnimDict('random@arrests')
  while not HasAnimDictLoaded('random@arrests') do
    Wait(0)
  end
  RequestAnimDict('random@arrests@busted')
  while not HasAnimDictLoaded('random@arrests@busted') do
    Wait(0)
  end
  if not (IsEntityPlayingAnim( targetPed, "random@arrests", "idle_2_hands_up", 3 )) or not (IsEntityPlayingAnim( targetPed, "random@arrests", "kneeling_arrest_idle", 3 )) or not (IsEntityPlayingAnim( targetPed, "random@arrests@busted", "enter", 3 )) or not (IsEntityPlayingAnim( targetPed, "random@arrests@busted", "idle_a", 3 )) then
    TaskPlayAnim( targetPed, "random@arrests", "idle_2_hands_up", 8.0, 1.0, -1, 2, 0, 0, 0, 0 )
    Wait (4000)
    TaskPlayAnim( targetPed, "random@arrests", "kneeling_arrest_idle", 8.0, 1.0, -1, 2, 0, 0, 0, 0 )
    Wait (500)
    TaskPlayAnim( targetPed, "random@arrests@busted", "enter", 8.0, 1.0, -1, 2, 0, 0, 0, 0 )
    Wait (1000)
    TaskPlayAnim( targetPed, "random@arrests@busted", "idle_a", 8.0, 1.0, -1, 9, 0, 0, 0, 0 )
  end
end

Hi, this reply wasn’t for me, but I found it pretty helpful so thanks.
I’ve been at this for a ‘bit’ though and I’m not sure what I’m missing, maybe you can help?
I only assume I’m getting the 'attempt to call a nil value (global ‘PlayerId’) error because I’m not using the beginning ESX bit?

local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId())

Remove the -1

Thanks, anders. I actually took a step away from this for a bit, but I appreciate the catch on that. Embarrassingly enough, my mistake was that I had ‘PlayerPedId()’ instead of 'PlayerId(). I tested it with -1 and it actually worked as well.

Again, thanks for the help. :smile:

IsPlayerFreeAiming(PlayerId(PlayerPedId()))

This should work :smile: