LUA - Need Help With Adding Keybind To Client Script For IDs

Would anyone know how I can show the IDs as such by default but toggle them off with a command? (eg. /toggleids)

I’m aware there are a handful of other scripts out there but most of them are now 3-5 years old and no longer work properly. Trust me I’ve been trying for two days to get one working. So I figure maybe I will just ask for some lua help here because I don’t know too much about lua. Thanks in advance!

local disPlayerNames = 5
local playerDistances = {}

local function DrawText3D(position, text, r,g,b) 
    local onScreen,_x,_y=World3dToScreen2d(position.x,position.y,position.z+1)
    local dist = #(GetGameplayCamCoords()-position)
 
    local scale = (1/dist)*2
    local fov = (1/GetGameplayCamFov())*100
    local scale = scale*fov
   
    if onScreen then
        if not useCustomScale then
            SetTextScale(0.0*scale, 0.45*scale)
        else 
            SetTextScale(0.0*scale, customScale)
        end
        SetTextFont(0)
        SetTextProportional(1)
        SetTextColour(r, g, b, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(2, 0, 0, 0, 150)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x,_y)
    end
end

Citizen.CreateThread(function()
	Wait(500)
    while true do
        for _, id in ipairs(GetActivePlayers()) do
            local targetPed = GetPlayerPed(id)
            if targetPed ~= PlayerPedId() then
                if playerDistances[id] then
                    if playerDistances[id] < disPlayerNames then
                        local targetPedCords = GetEntityCoords(targetPed)
                        if NetworkIsPlayerTalking(id) then
                            DrawText3D(targetPedCords, GetPlayerServerId(id), 102,155,105)
                        else
                            DrawText3D(targetPedCords, GetPlayerServerId(id), 192,192,192)
                        end
                    end
                end
            end
        end
        Citizen.Wait(0)
    end
end)

Citizen.CreateThread(function()
    while true do
        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        
        for _, id in ipairs(GetActivePlayers()) do
            local targetPed = GetPlayerPed(id)
            if targetPed ~= playerPed then
                local distance = #(playerCoords-GetEntityCoords(targetPed))
				playerDistances[id] = distance
            end
        end
        Wait(1000)
    end
end)

This one is now the working snip.

The key binds with the + - on your other post are for when it is held and released, but as we are working with loops, might be easier as just a normal mapping toggle.


local function DrawText3D(position, text, r, g, b)
    local onScreen, _x, _y = World3dToScreen2d(position.x, position.y, position.z + 1)
    local dist = #(GetGameplayCamCoords() - position)

    local scale = (1 / dist) * 2
    local fov = (1 / GetGameplayCamFov()) * 100
    local scale = scale * fov

    if onScreen then
        if not useCustomScale then
            SetTextScale(0.0 * scale, 0.45 * scale)
        else
            SetTextScale(0.0 * scale, customScale)
        end
        SetTextFont(0)
        SetTextProportional(1)
        SetTextColour(r, g, b, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(2, 0, 0, 0, 150)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x, _y)
    end
end

---
-- @param ords vec "GetEntityCoords(PlayerPedId())"
-- @param radius number "distance around point"
local function GetPlayersInArea(ords, radius)
    local coords = vector3(ords)
    local objs = GetGamePool("CPed")
    local obj = {}
        for _, v in pairs(objs) do
            if IsPedAPlayer(v) then
                local target = vector3(GetEntityCoords(v))
                local distance = #(target - coords)
                if distance <= radius then
                    table.insert(obj, NetworkGetPlayerIndexFromPed(v))
                end
            end
        end -- {obj,obj,obj}
        -- include self
        table.insert(obj, PlayerId())
    return obj
end

local index = {}

Citizen.CreateThread(function()
    while true do
        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        index = GetPlayersInArea(playerCoords, 35)
        Wait(2500)
    end
end)

local displayNames = false

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if displayNames then
            for _, id in pairs(index) do
                if NetworkIsPlayerTalking(id) then
                    DrawText3D(GetEntityCoords(GetPlayerPed(id)), id, 102, 155, 105)
                else
                    DrawText3D(GetEntityCoords(GetPlayerPed(id)), id, 192, 192, 192)
                end
            end
        else
            Citizen.Wait(1250)
        end
    end
end)

RegisterCommand("showid", function()
    displayNames = not displayNames
end)
RegisterKeyMapping("showid", "Show IDs", "keyboard", "F4")

My reply on your other post is pending so here is a working thing.
@LegacysMaps

Only tested with myself, I should add :smiley:

.lua:49: expected near ‘end’

        table.insert(obj, PlayerId())  --49?
    end
    return obj
end

syntax error here the checker says?

I probably left one end in there frommy original function…

Rip

1 Like

SOLVED (sort of) with a keybind. Albeit not exactly what I wanted.

I was hoping for RegisterKeyMapping as well so people could change the keybind to whatever they wanted. But I wasn’t sure how to set that up. But this will do for now.

disPlayerNames = 5 --distance you see IDs at 
keyToToggleIDs = 19 --left alt by default

playerDistances = {}
showIDsAboveHead = false

Citizen.CreateThread(function()
    while true do 
        if IsControlJustPressed(0, keyToToggleIDs) then
            showIDsAboveHead = not showIDsAboveHead
			print("changed")
            Wait(50)
        end
        Wait(0)
    end
end)

Citizen.CreateThread(function()
    while true do
        for id = 0, 255 do
            if GetPlayerPed(id) ~= GetPlayerPed(-1) then
                x1, y1, z1 = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
                x2, y2, z2 = table.unpack(GetEntityCoords(GetPlayerPed(id), true))
                distance = math.floor(GetDistanceBetweenCoords(x1,  y1,  z1,  x2,  y2,  z2,  true))
                playerDistances[id] = distance
            end
        end
        Citizen.Wait(1000)
    end
end)

Citizen.CreateThread(function()
    while true do
        if showIDsAboveHead then
            for id = 0, 255 do 
                if NetworkIsPlayerActive(id) then
                    if GetPlayerPed(id) ~= GetPlayerPed(-1) then
                        if (playerDistances[id] < disPlayerNames) then
                            x2, y2, z2 = table.unpack(GetEntityCoords(GetPlayerPed(id), true))
                            if NetworkIsPlayerTalking(id) then
                                DrawText3D(x2, y2, z2+1, GetPlayerServerId(id), 105,155,102)
                            else
                                DrawText3D(x2, y2, z2+1, GetPlayerServerId(id), 255,255,255)
                            end
                        end  
                    end
                end
            end
        end
        Citizen.Wait(0)
    end
end)

function DrawText3D(x,y,z, text, r,g,b) 
    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
    local px,py,pz=table.unpack(GetGameplayCamCoords())
    local dist = GetDistanceBetweenCoords(px,py,pz, x,y,z, 1)
 
    local scale = (1/dist)*2
    local fov = (1/GetGameplayCamFov())*100
    local scale = scale*fov
   
    if onScreen then
        SetTextScale(0.0*scale, 0.55*scale)
        SetTextFont(0)
        SetTextProportional(1)
        SetTextColour(r, g, b, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(2, 0, 0, 0, 150)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x,_y)
    end
end

What @Twittchter did would have been fine, you just had to remove the single extra end.

Here is it is with the command and keybind you wanted:

disPlayerNames = 5 --distance you see IDs at

playerDistances = {}
showIDsAboveHead = false

Citizen.CreateThread(function()
    while true do
        for id = 0, 255 do
            if GetPlayerPed(id) ~= GetPlayerPed(-1) then
                x1, y1, z1 = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
                x2, y2, z2 = table.unpack(GetEntityCoords(GetPlayerPed(id), true))
                distance = math.floor(GetDistanceBetweenCoords(x1,  y1,  z1,  x2,  y2,  z2,  true))
                playerDistances[id] = distance
            end
        end
        Citizen.Wait(1000)
    end
end)

Citizen.CreateThread(function()
    while true do
        if showIDsAboveHead then
            for id = 0, 255 do
                if NetworkIsPlayerActive(id) then
                    if GetPlayerPed(id) ~= GetPlayerPed(-1) then
                        if (playerDistances[id] < disPlayerNames) then
                            x2, y2, z2 = table.unpack(GetEntityCoords(GetPlayerPed(id), true))
                            if NetworkIsPlayerTalking(id) then
                                DrawText3D(x2, y2, z2+1, GetPlayerServerId(id), 105,155,102)
                            else
                                DrawText3D(x2, y2, z2+1, GetPlayerServerId(id), 255,255,255)
                            end
                        end
                    end
                end
            end
        end
        Citizen.Wait(0)
    end
end)

function DrawText3D(x,y,z, text, r,g,b)
    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
    local px,py,pz=table.unpack(GetGameplayCamCoords())
    local dist = GetDistanceBetweenCoords(px,py,pz, x,y,z, 1)

    local scale = (1/dist)*2
    local fov = (1/GetGameplayCamFov())*100
    local scale = scale*fov

    if onScreen then
        SetTextScale(0.0*scale, 0.55*scale)
        SetTextFont(0)
        SetTextProportional(1)
        SetTextColour(r, g, b, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(2, 0, 0, 0, 150)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x,_y)
    end
end

RegisterKeyMapping('toggleids', 'Toggle IDs above head', 'keyboard', 'LMENU')
RegisterCommand('toggleids', function()
	showIDsAboveHead = not showIDsAboveHead
end)
1 Like