Skip to content

Commit cb986b3

Browse files
Add player ID display and online count script
Initial commit of the cdl-showid resource for QB-Core. Adds client and server scripts to display player IDs overhead and show total online players, with configuration options and documentation. Includes preview image and setup instructions.
0 parents  commit cb986b3

6 files changed

Lines changed: 231 additions & 0 deletions

File tree

client/main.lua

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
QBCore = exports['qb-core']:GetCoreObject()
2+
local forceDraw = false
3+
local shouldDraw = false
4+
local totalOnlinePlayers = 0
5+
local fetchOnlineThreadRunning = false
6+
7+
-- Ensure QBCore is loaded and register toggle command
8+
Citizen.CreateThread(function()
9+
while QBCore == nil do
10+
TriggerEvent('QBCore:GetObject', function(obj) QBCore = obj end)
11+
Citizen.Wait(0)
12+
end
13+
14+
-- Toggle IDs via keybind (default U)
15+
RegisterCommand('toggleShowID', function()
16+
shouldDraw = not shouldDraw
17+
18+
-- Optional thinking animation
19+
if Config.Animation then
20+
local playerPed = PlayerPedId()
21+
if shouldDraw then
22+
loadAnimDict('misscarsteal4@aliens')
23+
TaskPlayAnim(playerPed, 'misscarsteal4@aliens', 'rehearsal_base_idle_director', 8.0, -8, -1, 49, 0, false, false, false)
24+
else
25+
ClearPedTasks(playerPed)
26+
end
27+
end
28+
end, false)
29+
30+
RegisterKeyMapping('toggleShowID', 'Toggle Player ID Display', 'keyboard', 'U')
31+
end)
32+
33+
-- Toggle via /id command (this may not work as there is another /id command in QBCore by default)
34+
RegisterNetEvent('cdl-showid:id')
35+
AddEventHandler('cdl-showid:id', function()
36+
forceDraw = not forceDraw
37+
end)
38+
39+
-- Helper functions
40+
function loadAnimDict(dict)
41+
while not HasAnimDictLoaded(dict) do
42+
RequestAnimDict(dict)
43+
Citizen.Wait(5)
44+
end
45+
end
46+
47+
-- Updated Draw3DText with dynamic vertical offset for close/far/high-angle cameras
48+
function Draw3DText(x, y, z, text)
49+
local camCoords = GetGameplayCamCoords()
50+
local dist = GetDistanceBetweenCoords(camCoords, x, y, z, true)
51+
52+
-- Dynamic vertical offset: lowers text for distant players
53+
local zOffset = 1.1 - ((dist - 5.0) * 0.08) -- reduce offset with distance
54+
if dist < 5.0 then
55+
zOffset = 0.9 -- close camera → slightly lower
56+
elseif zOffset < 0.6 then
57+
zOffset = 0.6 -- minimum height so it doesn't go too low
58+
end
59+
60+
local onScreen, _x, _y = World3dToScreen2d(x, y, z + zOffset)
61+
if onScreen then
62+
local scale = 1.2 * (1 / dist) * (1 / GetGameplayCamFov()) * 100
63+
SetTextScale(scale, scale)
64+
SetTextFont(4)
65+
SetTextProportional(1)
66+
SetTextColour(255, 255, 255, 255)
67+
SetTextDropShadow(0, 0, 0, 0, 255)
68+
SetTextEdge(4, 0, 0, 0, 255)
69+
SetTextOutline()
70+
SetTextEntry("STRING")
71+
SetTextCentre(1)
72+
AddTextComponentString(text)
73+
DrawText(_x, _y)
74+
end
75+
end
76+
77+
function GetNearestPlayers()
78+
local playerPed = PlayerPedId()
79+
local players = QBCore.Functions.GetPlayers(GetEntityCoords(playerPed), Config.DrawDistance)
80+
local players_clean = {}
81+
82+
for i = 1, #players do
83+
table.insert(players_clean, {
84+
playerName = GetPlayerName(players[i]),
85+
playerId = GetPlayerServerId(players[i]),
86+
coords = GetEntityCoords(GetPlayerPed(players[i]))
87+
})
88+
end
89+
return players_clean
90+
end
91+
92+
-- Fetch online players every 2 seconds when IDs are active
93+
local function startOnlineFetch()
94+
if fetchOnlineThreadRunning then return end
95+
fetchOnlineThreadRunning = true
96+
97+
Citizen.CreateThread(function()
98+
while shouldDraw or forceDraw do
99+
QBCore.Functions.TriggerCallback('cdl-showid:getOnlinePlayers', function(count)
100+
if count then
101+
totalOnlinePlayers = count
102+
end
103+
end)
104+
Citizen.Wait(2000)
105+
end
106+
fetchOnlineThreadRunning = false
107+
end)
108+
end
109+
110+
-- Draw IDs and two-line ONLINE count
111+
Citizen.CreateThread(function()
112+
while true do
113+
Citizen.Wait(0)
114+
if shouldDraw or forceDraw then
115+
startOnlineFetch()
116+
117+
-- Draw nearby player IDs
118+
local nearbyPlayers = GetNearestPlayers()
119+
for _, v in pairs(nearbyPlayers) do
120+
local x, y, z = table.unpack(v.coords)
121+
Draw3DText(x, y, z, v.playerId)
122+
end
123+
124+
-- Right-middle ONLINE display (two lines, big & bold)
125+
local xPos = 0.963 -- right side
126+
local yPos = 0.43 -- header slightly above vertical center
127+
local headerText = "ONLINE:"
128+
local countText = tostring(totalOnlinePlayers)
129+
130+
-- Header (ONLINE:)
131+
SetTextFont(4)
132+
SetTextProportional(1)
133+
SetTextScale(0.6, 0.6) -- header bigger
134+
SetTextColour(255, 255, 255, 255)
135+
SetTextDropshadow(2, 0, 0, 0, 255)
136+
SetTextEdge(4, 0, 0, 0, 255)
137+
SetTextOutline()
138+
SetTextCentre(true)
139+
SetTextEntry("STRING")
140+
AddTextComponentString(headerText)
141+
DrawText(xPos, yPos)
142+
143+
-- Count (number) slightly below header
144+
SetTextFont(4)
145+
SetTextProportional(1)
146+
SetTextScale(0.55, 0.55) -- slightly smaller than header
147+
SetTextColour(255, 255, 255, 255)
148+
SetTextDropshadow(2, 0, 0, 0, 255)
149+
SetTextEdge(4, 0, 0, 0, 255)
150+
SetTextOutline()
151+
SetTextCentre(true)
152+
SetTextEntry("STRING")
153+
AddTextComponentString(countText)
154+
DrawText(xPos, yPos + 0.032) -- small vertical gap
155+
end
156+
end
157+
end)
158+
159+
-- Thankyou if you are seeing this :-)

config.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Config = {}
2+
3+
-- Distance to search for nearby players
4+
Config.DrawDistance = 1
5+
6+
-- Only admins can use the toggle (optional)
7+
Config.AdminOnly = false
8+
9+
-- Enable thinking animation when showing IDs
10+
Config.Animation = true
11+
12+
-- Show a list of nearby players? (optional, for future use)
13+
Config.ShowList = true

fxmanifest.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fx_version 'cerulean'
2+
game 'gta5'
3+
4+
name 'cdl-showid' -- updated name
5+
author 'CyberDEVLab'
6+
description 'Displays player ID overhead and shows total players'
7+
version '1.0.1'
8+
9+
client_scripts {
10+
'config.lua',
11+
'client/main.lua'
12+
}
13+
14+
server_scripts {
15+
'config.lua',
16+
'server/main.lua'
17+
}
18+
19+
-- Note:
20+
-- Keybind is now handled via FiveM Settings → Key Bindings → FiveM.
21+
-- The default key is 'U' but players can change it in-game.

preview.png

2.43 MB
Loading

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# cdl-showid
2+
[![Made with Unity](https://img.shields.io/badge/official-CYBERdevLab-FFFFFF.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/CYBERdevLab)
3+
4+
A lightweight and efficient **QB-Core** script that displays player IDs above players’ heads and shows the total number of active players in the server.
5+
6+
## ⭐ Features
7+
- Toggle player ID display using **U**.
8+
- Player count shown on the right side of the screen.
9+
- Lightweight, optimized, and stable for production servers.
10+
11+
## 📦 Installation
12+
1. Download the latest release from the GitHub repository (see **[Releases](https://github.com/CYBERdevLab/cdl-showid/releases)**).
13+
2. Place the resource folder in `resources/`.
14+
3. Add the following line to your `server.cfg`: `ensure cdl-showid`
15+
4. Restart your server or start the resource manually: `start cdl-showid`
16+
17+
## 🖼️ Preview
18+
19+
<details>
20+
<summary>Click to Preview Image</summary>
21+
<img src="/preview.png" alt="Preview" width="70%" />
22+
</details>
23+
24+
## 👤 Author
25+
- Name: Shaswat_Acharya
26+
- Role: Developer / Hobby Projects
27+
- Contact: [Github](https://github.com/shaswatacharya)
28+
29+
>[!IMPORTANT]
30+
>**From Author**: This is an independent side project created out of necessity and does not reflect my primary field of interest. I’m sharing it in case it benefits others. If it stops working in future versions, please adjust or update it as needed.😊

server/main.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
QBCore = nil
2+
QBCore = exports['qb-core']:GetCoreObject()
3+
TriggerEvent('QBCore:GetObject', function(obj) QBCore = obj end)
4+
5+
QBCore.Functions.CreateCallback('cdl-showid:getOnlinePlayers', function(source, cb)
6+
local players = QBCore.Functions.GetPlayers()
7+
cb(#players) -- returns total number of connected players
8+
end)

0 commit comments

Comments
 (0)