Trying to make a id system

i was trying to make an citizen id in my sever here how i coded it!



function randomcid()
if ply:GetNWInt("cid") == 0 then
ply:SetNWInt("cid", math.random( 1,9999 ))
end
end


What’s the problem?

If I’m correct he’s probably getting duplicate ID numbers because he doesn’t have any system to detect whether an ID number is claimed or not

Are you trying to make their IDs permanent or just temporary?

And ply:UniqueID( ) might work well too

im trying to make do a random number for the id about 4 digits and then appear on the hud



function myhud()
//main hud
local hudx = ScrW() - 1150
local hudy = ScrH() - 100
local healthx = ScrW() - 1150
local healthy = ScrH() - 90
local pingx = ScrW() - 1150
local pingy = ScrH() - 80
local mlevelx = ScrW() - 1150
local mlevely = ScrH() - 60
local mxpx = ScrW() - 1150
local mxpy = ScrH() - 40
//xp bar
local xpx = ScrW() - 850
local xpy = ScrH() - 150
local levelx = ScrW() - 560
local levely = ScrH() - 200

	local client = LocalPlayer()
	if !client:Alive() then return end
	if(client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
	draw.RoundedBox(3, hudx, hudy, 200, 100, Color(51, 58, 51, 255)) // Main Hud BackGround
	draw.RoundedBox(3, xpx, xpy, 600, 50, Color(51, 58, 51, 255)) // Bar background
	draw.RoundedBox(3, xpx, xpy, math.Clamp( client:GetNWInt("xp"), 0, 300)*2, 50, Color(0, 255, 0, 255)) //Xp Bar
	draw.SimpleText("Health: " .. client:Health(), "Default", healthx, healthy, Color(255, 0, 0, 255), 0, 0) //health
	draw.SimpleText(client:GetNWInt("level"), "DermaLarge", levelx, levely, Color(0, 0, 255, 255), 0, 0) // level
	draw.SimpleText("Ping: ".. client:Ping(), "Default", pingx, pingy, Color(255, 255, 255, 255), 0, 0) // ping
	draw.SimpleText("Level: ".. client:GetNWInt("level"), "Default", mlevelx, mlevely, Color(0, 0, 255, 200), 0, 0) // level
	draw.SimpleText("XP: ".. client:GetNWInt("xp"), "Default", mxpx, mxpy, Color(21, 21, 255, 200), 0, 0) // xp
	local mag_left = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
	local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
	local secondary_ammo = client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType())// How much ammunition you have for your secondary fire, such as the MP7's grenade launcher
end
hook.Add("HUDPaint", "myhud", myhud)



IdentifiedPlayers = {}
hook.Add("PlayerSpawn", "GiveID", function( ply )

if ply:GetNWInt("cid") == 0 then
local guessid = math.random(1,9999)

for k, v in pairs(IdentifiedPlayers) do
if guessid == v then
repeat
guessid = math.random(1,9999)
until !(guessid == v)
end
end
table.insert(IdentifiedPlayers, guessid, guessid)
ply:SetNWInt("cid", guessid)
end
end)

That’s my best guess

I’d do something along the lines of this.
[lua]
local Player = FindMetaTable(“Player”)
if !Player then return end

if SERVER then
AddCSLuaFile()
PlayerIDs = PlayerIDs or {}

function Player:SetIdentity(num)
	ClearOutInvalidIDs()
	if !PlayerIDs[num] then
		if PlayerIDs[self:GetIdentity()] then
			PlayerIDs[self:GetIdentity()] = nil
		end
		
		self:SetNWInt("cid", num)
		PlayerIDs[num] = self:EntIndex()
	else
		self:SetIdentity(math.random(1,9999))
	end
end

local function GetPlayerByIdentity(num)
	local playerID = PlayerIDs[num]
	if playerID then
		local plyByIdentity = Entity(playerID)
		if !IsValid(plyByIdentity) then 
			PlayerIDs[num] = nil 
			return nil
		end
		return plyByIdentity
	end
end

local function ClearOutInvalidIDs()
	for k,v in pairs(PlayerIDs) do
		GetPlayerByIdentity(k)
	end
end

hook.Add("PlayerInitialSpawn", "PlayerIDs.PlayerInitialSpawn", function(ply)
	local randID = math.random(1,9999)
	ply:SetIdentity(randID)
	print(GetPlayerByIdentity(randID))
end)

end

function Player:GetIdentity()
return self:GetNWInt(“cid”, 0)
end
[/lua]

YOU ARE THE KING OF LUA thank so much it works!!!