Jump to content

[HELP] Target marking system


Ryuto

Recommended Posts

Hello!!

I was able to create a player selection system but I have a small error.

This system only selects a specific player and there is no way to select other nearby players.

Is there another way to select other players?

For example, by pressing a button or pressing the mouse wheel you can select another nearby player.

This is my script code.

 

local playerSelectedTarget
local alpha = 255


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 playerSelectedTarget ~= localPlayer and ( distance < 30 ) then --target distanc
                        playerSelectedTarget = target
                        local alpha = 255
                        setElementData(localPlayer, "Select", true)
                    end
                end    
            end
            if isElement(playerSelectedTarget) then        
                local Px,Py,Pz = getCameraMatrix( )
                local zhx,zhy,zhz = getPedBonePosition(playerSelectedTarget,3)
                local dist = getDistanceBetweenPoints3D ( Px, Py, Pz, zhx,zhy,zhz )
                if playerSelectedTarget ~= localPlayer and ( dist < 30 ) then --target distanc
                if ( isLineOfSightClear ( Px, Py, Pz, zhx,zhy,zhz, true, true, false, false, true, false, false, playerSelectedTarget ) ) then
                local sxx,syy = getScreenFromWorldPosition(zhx,zhy,zhz+0.3)
                if sxx and syy then                        
                 local size = 1300  / dist
                   dxDrawImage( sxx - ( size / 2 ), syy - ( size / 4.5 ), size, size, "Effect/Select.webp", 0, 0, 0, tocolor ( 255, 255, 255, alpha ),false) 
                   end
                end
             end
            end
        else
            if isElement(playerSelectedTarget) then        
            local alpha = 0
            setElementData(localPlayer, "Select", false)
            end    
        end        
    end
)

I leave a video of my problem so you can see the error.

 

 

thanks for reading me!!

Link to comment

To detect the player you are using a distance less than 30, but the problem is that there are several players near you with a distance less than 30. Therefore, it will always take the last one on the list whose distance is less than 30

You should check who is closest and what is inside the player's camera.

addEventHandler("onClientRender", root, 
  function()
    local maxDistance = 30 
    for _, player in ipairs(...)
    ... 
       if distance <= maxDistance and isElementOnScreen(player) then 
          maxDistance = distance
          playerSelectedTarget = player 
       end 
    end 
 end 
)

 

Edited by alex17"
Link to comment
17 hours ago, alex17&quot; said:

To detect the player you are using a distance less than 30, but the problem is that there are several players near you with a distance less than 30. Therefore, it will always take the last one on the list whose distance is less than 30

You should check who is closest and what is inside the player's camera.

addEventHandler("onClientRender", root, 
  function()
    local maxDistance = 30 
    for _, player in ipairs(...)
    ... 
       if distance <= maxDistance and isElementOnScreen(player) then 
          maxDistance = distance
          playerSelectedTarget = player 
       end 
    end 
 end 
)

 

I guess he wants the ability to cycle through and select different nearby players and not always target the closet one on the screen

 

local playerSelectedTarget
local alpha = 255
local nearbyPlayers = {}
local selectedIndex = 1

addEventHandler("onClientRender", root,
    function()
        if getKeyState("mouse2") then
            local x, y, z = getElementPosition(localPlayer)
            nearbyPlayers = {} -- reset the table
            for _, target in ipairs(getElementsByType("player", root, true)) do
                if target ~= localPlayer then
                    local px, py, pz = getElementPosition(target)
                    local distance = getDistanceBetweenPoints3D(px, py, pz, x, y, z)
                    if distance < 30 then
                        table.insert(nearbyPlayers, target)
                    end
                end
            end
            playerSelectedTarget = nearbyPlayers[selectedIndex]
            if isElement(playerSelectedTarget) then
                -- ur code to display the selection effect
            end
        else
            if isElement(playerSelectedTarget) then
                alpha = 0
                setElementData(localPlayer, "Select", false)
            end
        end
    end
)

addEventHandler("onClientKey", root,
    function(button, press)
        if button == "mouse_wheel_up" and press then
            selectedIndex = selectedIndex - 1
            if selectedIndex < 1 then
                selectedIndex = #nearbyPlayers
            end
        elseif button == "mouse_wheel_down" and press then
            selectedIndex = selectedIndex + 1
            if selectedIndex > #nearbyPlayers then
                selectedIndex = 1
            end
        end
    end
)

 

Link to comment
  • 1 month later...
  • Moderators
17 hours ago, Ryuto said:

I'm sorry to reopen this post again but I have a question.

Is there a way to detect if the player is being selected from another external script so I can execute a function?

Yes, you use the event system for that.

--[[
	Ⓒ FLUSHBICEPS
]]

addEventHandler("onClientKey", root,
	function(button, press)
		if not press then return end
		if button == "mouse_wheel_up" then
			selectedIndex = selectedIndex - 1
			if selectedIndex < 1 then
				selectedIndex = #nearbyPlayers
			end
		elseif button == "mouse_wheel_down" then
			selectedIndex = selectedIndex + 1
			if selectedIndex > #nearbyPlayers then
				selectedIndex = 1
			end
		else return end

		local selectedPlayer = nearbyPlayers[selectedIndex]
		if not isElement(selectedPlayer) then return end
		triggerEvent("onClientSelectPlayer", selectedPlayer, selectedIndex)
    end
)

 

addEvent('onClientSelectPlayer', false)
addEventHandler('onClientSelectPlayer', root, 
function (selectedIndex)
	iprint('Player-element:', source, ', index:', selectedIndex)
end)

 

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