[ESX] [HELP] Select a player with the mouse cursor

Hi everyone, I would like to do something different that I have seen a bit in some servers, that is, select a player when you give an object via the mouse cursor but I can’t find any documentation about it, so I would like to ask you something,

At the moment I managed to do this alone but it gives me several problems also based on precision

ESX = exports["es_extended"]:getSharedObject()


local selectingPlayer = false
local highlightedPlayer = nil

RegisterCommand("selezionamouse", function()
    selectingPlayer = true
    SetNuiFocus(true, true)
    SetCursorLocation(0.5, 0.5)
    ESX.ShowNotification("Clicca su un giocatore per selezionarlo.")
end, false)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        if selectingPlayer then

            ShowCursorThisFrame()

            DisableControlAction(0, 140, true) 
            DisableControlAction(0, 141, true)
            DisableControlAction(0, 142, true) 

            local entityHit = getEntityInFrontOfPlayer(10.0)

            if entityHit and IsEntityAPed(entityHit) and NetworkIsPlayerActive(NetworkGetEntityOwner(entityHit)) then
                local playerId = NetworkGetEntityOwner(entityHit)
                highlightedPlayer = playerId

                DrawMarkerOnPlayer(playerId)

                if IsControlJustPressed(0, 24) then 
                    ESX.ShowNotification("Hai selezionato il giocatore: " .. GetPlayerName(playerId))
                    
                    SetNuiFocus(false, false) 
                    selectingPlayer = false
                    highlightedPlayer = nil
                end
            else
                highlightedPlayer = nil 
            end
        end
    end
end)

function getEntityInFrontOfPlayer(maxDistance)
    local playerPed = PlayerPedId()
    local playerCoords = GetEntityCoords(playerPed)
    local forwardVector = GetEntityForwardVector(playerPed)
    local rayStart = playerCoords + vector3(0.0, 0.0, 0.5)
    local rayEnd = rayStart + (forwardVector * maxDistance)

    local rayHandle = StartShapeTestRay(rayStart.x, rayStart.y, rayStart.z, rayEnd.x, rayEnd.y, rayEnd.z, 8, playerPed, 0)
    local _, hit, _, _, entityHit = GetShapeTestResult(rayHandle)

    if hit then
        return entityHit
    else
        return nil
    end
end

function DrawMarkerOnPlayer(playerId)
    local targetPed = GetPlayerPed(playerId)
    local targetCoords = GetEntityCoords(targetPed)
    local headCoords = GetPedBoneCoords(targetPed, 0x796e) 


    DrawText3D(headCoords.x, headCoords.y, headCoords.z + 1.0, GetPlayerName(playerId))
end

function DrawText3D(x, y, z, text)
    local onScreen, _x, _y = World3dToScreen2d(x, y, z)
    local px, py, pz = table.unpack(GetGameplayCamCoords())

    if onScreen then
        SetTextScale(0.35, 0.35)
        SetTextFont(4)
        SetTextProportional(1)
        SetTextColour(255, 255, 255, 215)
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x, _y)

        local factor = (string.len(text)) / 370
        DrawRect(_x, _y + 0.0125, 0.015 + factor, 0.03, 41, 11, 41, 68)
    end
end

WOW THX, It’s working but the logo on the player is not showing =(

Has “Marker Loaded” been printed in F8 console?

Yes, I didn’t change anything, simply downloaded, inserted and started, obviously I put the ox part and the getId part to see if the marker appeared

Strange

Now it works, I didn’t know that the marker only appeared on players and not on NPCs

Perfect! Keep it up

You can enable targeting on npc if you want. Read the documentation, you must put npc parameter to true in the export