Jump to content

[HELP] Target Distance


Ryuto

Recommended Posts

I would like to know if there is any way to be able to aim or select a player regardless of whether they are far away with the mouse2 or the right button, without the need for a weapon. What functions could I use?

This is what I want to try to achieve:

 

 

Link to comment
local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
    function()
        if ( getKeyState("mouse2") == true ) then
		    local x,y,z = getElementPosition(localPlayer)
            for k,target in ipairs(getElementsByType("player",root,true)) do
			    if not ( target == localPlayer ) then
                    local px,py,pz = getElementPosition(target)	
                    local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
                    if ( distance < 10 ) then --target distance
                        playerSelectedTarget = target
                    end
				end	
			end
			if isElement(playerSelectedTarget) then
			    local tx,ty,tz = getElementPosition(playerSelectedTarget)
                setElementPosition( targetMarker, tx, ty, tz+3 ) 			
			end
		else
			if isElement(playerSelectedTarget) then
                setElementPosition( targetMarker, 0, 0, 10000 ) 			
			end		
        end		
	end
)	

 

  • Thanks 1
Link to comment
  • 1 month later...
On 1/2/2023 at 1:40 PM, KaMi said:
local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
    function()
        if ( getKeyState("mouse2") == true ) then
		    local x,y,z = getElementPosition(localPlayer)
            for k,target in ipairs(getElementsByType("player",root,true)) do
			    if not ( target == localPlayer ) then
                    local px,py,pz = getElementPosition(target)	
                    local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
                    if ( distance < 10 ) then --target distance
                        playerSelectedTarget = target
                    end
				end	
			end
			if isElement(playerSelectedTarget) then
			    local tx,ty,tz = getElementPosition(playerSelectedTarget)
                setElementPosition( targetMarker, tx, ty, tz+3 ) 			
			end
		else
			if isElement(playerSelectedTarget) then
                setElementPosition( targetMarker, 0, 0, 10000 ) 			
			end		
        end		
	end
)	

 

It worked perfect for me, thank you.

I have another question, how can I use that code to detect if the player is being selected? And if so, what method can I use to make the selected player do an animation?

Link to comment
--server-side code
addEvent("doAnimation", true)
addEventHandler("doAnimation", root,
function(target)
setPedAnimation(target, "CRACK", "crckdeth2", -1, false, false, false, false)
setTimer( function() setPedAnimation(target) end, 10000, 1 ) -- stop animation after 10 seconds
end
)

--client-side code
local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
function()
if ( getKeyState("mouse2") == true ) then
local x,y,z = getElementPosition(localPlayer)
for k,target in ipairs(getElementsByType("player",root,true)) do
if not ( target == localPlayer ) then
local px,py,pz = getElementPosition(target)
local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
if ( distance < 10 ) then --target distance
playerSelectedTarget = target
triggerServerEvent("doAnimation", root, playerSelectedTarget) -- trigger server event
end
end
end
if isElement(playerSelectedTarget) then
local tx,ty,tz = getElementPosition(playerSelectedTarget)
setElementPosition( targetMarker, tx, ty, tz+3 )
end
else
if isElement(playerSelectedTarget) then
setElementPosition( targetMarker, 0, 0, 10000 )
end
end
end
)

 

Link to comment

 

local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
    function()
        if ( getKeyState("mouse2") == true ) then
		    local x,y,z = getElementPosition(localPlayer)
            for k,target in ipairs(getElementsByType("player",root,true)) do
			    if not ( target == localPlayer ) then
                    local px,py,pz = getElementPosition(target)	
                    local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
                    if ( distance < 10 ) then --target distance
                        playerSelectedTarget = target
                    end
				end	
			end
			if isElement(playerSelectedTarget) then
			    local tx,ty,tz = getElementPosition(playerSelectedTarget)
                setElementPosition( targetMarker, tx, ty, tz+3 ) 			
			end
		else
			if isElement(playerSelectedTarget) then
                setElementPosition( targetMarker, 0, 0, 10000 ) 			
			end		
        end		
	end
)	

 


Edited by Sr.black
Link to comment
On 2/8/2023 at 9:02 AM, FLUSHBICEPS said:
--server-side code
addEvent("doAnimation", true)
addEventHandler("doAnimation", root,
function(target)
setPedAnimation(target, "CRACK", "crckdeth2", -1, false, false, false, false)
setTimer( function() setPedAnimation(target) end, 10000, 1 ) -- stop animation after 10 seconds
end
)

--client-side code
local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
function()
if ( getKeyState("mouse2") == true ) then
local x,y,z = getElementPosition(localPlayer)
for k,target in ipairs(getElementsByType("player",root,true)) do
if not ( target == localPlayer ) then
local px,py,pz = getElementPosition(target)
local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
if ( distance < 10 ) then --target distance
playerSelectedTarget = target
triggerServerEvent("doAnimation", root, playerSelectedTarget) -- trigger server event
end
end
end
if isElement(playerSelectedTarget) then
local tx,ty,tz = getElementPosition(playerSelectedTarget)
setElementPosition( targetMarker, tx, ty, tz+3 )
end
else
if isElement(playerSelectedTarget) then
setElementPosition( targetMarker, 0, 0, 10000 )
end
end
end
)

 

 

How can I add something on the server side that will detect when a player is being targeted and when the targeting is detected run something like an animation.

I'm looking for something similar to the getPedTarget function but with this selection system.

 

I hope I explained myself well :(

Link to comment
--server-side code
addEvent("doAnimation", true)
addEvent("onTargetSelected", true) -- added trigger for player targeting
addEventHandler("onTargetSelected", root,
function(target)
setPedAnimation(target, "CRACK", "crckdeth2", -1, false, false, false, false)
setTimer( function() setPedAnimation(target) end, 10000, 1 ) -- stop animation after 10 seconds
end
)

--client-side code
local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
function()
if ( getKeyState("mouse2") == true ) then
local x,y,z = getElementPosition(localPlayer)
for k,target in ipairs(getElementsByType("player",root,true)) do
if not ( target == localPlayer ) then
local px,py,pz = getElementPosition(target)
local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
if ( distance < 10 ) then --target distance
playerSelectedTarget = target
triggerServerEvent("onTargetSelected", root, playerSelectedTarget) -- trigger server event
end
end
end
if isElement(playerSelectedTarget) then
local tx,ty,tz = getElementPosition(playerSelectedTarget)
setElementPosition( targetMarker, tx, ty, tz+3 )
end
else
if isElement(playerSelectedTarget) then
setElementPosition( targetMarker, 0, 0, 10000 )
end
end
end
)

 

Link to comment
On 11/02/2023 at 20:31, FLUSHBICEPS said:
--server-side code
addEvent("doAnimation", true)
addEvent("onTargetSelected", true) -- added trigger for player targeting
addEventHandler("onTargetSelected", root,
function(target)
setPedAnimation(target, "CRACK", "crckdeth2", -1, false, false, false, false)
setTimer( function() setPedAnimation(target) end, 10000, 1 ) -- stop animation after 10 seconds
end
)

--client-side code
local playerSelectedTarget
local targetMarker = createMarker( 0, 0, 10000, "arrow", 1.5, 255, 0, 0 )

addEventHandler("onClientRender", root,
function()
if ( getKeyState("mouse2") == true ) then
local x,y,z = getElementPosition(localPlayer)
for k,target in ipairs(getElementsByType("player",root,true)) do
if not ( target == localPlayer ) then
local px,py,pz = getElementPosition(target)
local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
if ( distance < 10 ) then --target distance
playerSelectedTarget = target
triggerServerEvent("onTargetSelected", root, playerSelectedTarget) -- trigger server event
end
end
end
if isElement(playerSelectedTarget) then
local tx,ty,tz = getElementPosition(playerSelectedTarget)
setElementPosition( targetMarker, tx, ty, tz+3 )
end
else
if isElement(playerSelectedTarget) then
setElementPosition( targetMarker, 0, 0, 10000 )
end
end
end
)

 

I really appreciate your help, little by little I'm seeing results.

I have a small problem, if I hold mouse2 down, the trigger repeats many times.

Isn't there another way to detect if the player is being selected without using the trigger?

something like an "if" condition to detect if the player is being selected from the serverside.

I have another question, is it possible to use a bind when selecting the player so that he performs an animation?

for example

I select it with mouse2 and if I press the number one button the selected player does an animation.

something like this:

 

Local HitPlayer = getPedTarget(thePlayer)
if not HitPlayer then return end

if HitPlayer and (getElementType(HitPlayer) = "ped" ) then

setPedAnimation

 

Link to comment
  • 6 months later...

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