Once the player leaves, they aren’t removed from the table

Once the player is dropped, i want the player to be removed from the GPlayerList table to prevent duplicates, but the player sticks to the table somehow, i just don’t understand.

Client Sided LUA Code:

CPlayerList = {}

local function toggleNuiFrame(shouldShow)
  SetNuiFocus(shouldShow, shouldShow)
  UIMessage('setVisible', shouldShow)
end

RegisterCommand('adminmenu', function()
  local PlayerList = lib.callback.await('vadmin:plist', false)

  if #PlayerList then
    UIMessage("nui:plist", PlayerList)
  end

  toggleNuiFrame(true)
end, false)

RegisterNUICallback('hideFrame', function(_, cb)
  toggleNuiFrame(false)
  Debug('Hide NUI frame')
  cb({})
end)

RegisterNUICallback('getClientData', function(data, cb)
  Debug('Data sent by React', json.encode(data))

  -- Lets send back client coords to the React frame for use
  local curCoords = GetEntityCoords(PlayerPedId())

  local retData <const> = { x = curCoords.x, y = curCoords.y, z = curCoords.z }
  cb(retData)
end)


RegisterNetEvent("UIMessage", function(action, data)
  UIMessage(action, data)
end)


RegisterNetEvent("vadmin:client:plist", function(id, playerData, initial)
  -- if not playerData then
  --   CPlayerList[id] = nil
  --   return
  -- end
  -- print(("[VAdmin] (PlayerData): %s"):format(json.encode(playerData)))
  print("Executed")
  if initial then
    CPlayerList = playerData
    UIMessage("nui:plist", playerData)
    return
  end


  CPlayerList[id] = playerData

  print(("[VAdmin] (CPlayerList): %s"):format(json.encode(CPlayerList)))


  -- for pids, pData in pairs(CPlayerList) do
  --   uploadData[#uploadData + 1] = {
  --     Name = pData.Name or "[Error] Unknown",
  --     ID = pData.ID,
  --     Health = pData.Health,
  --     Identifiers = pData.Identifiers,
  --     HWIDS = pData.HWIDS
  --   }
  -- end
  UIMessage("nui:plist", CPlayerList)
end)

RegisterNuiCallback("vadmin:nui_cb:ban", function(data, cb)
  print(("[VAdmin] (vadmin:nui_cb:ban): %s"):format(data))
  cb({})
end)

Server Sided LUA Code:

GPlayerList = {}

local function GetPlayerIdentifiersWithoutIP(player)
  local identifiers = GetPlayerIdentifiers(player)
  local cleanedIdentifiers = {}
  for _, identifier in ipairs(identifiers) do
    if not string.find(identifier, "ip:") then
      table.insert(cleanedIdentifiers, identifier)
    end
  end
  return cleanedIdentifiers
end

AddEventHandler("playerJoining", function(srcString, _oldID)
  if source <= 0 then
    print("[VAdmin (Server)] Error: 4")
    return
  end

  local playerDetectedName = GetPlayerName(source)

  if type(playerDetectedName) ~= "string" then
    print("[VAdmin (Server)] Error: 11")
    return
  end

  local playerData = {
    Name = string.sub(playerDetectedName or "unknown", 1, 75),
    ID = source,
    Identifiers = GetPlayerIdentifiersWithoutIP(source),
    HWIDS = GetPlayerTokens(source),
  }

  if GPlayerList[source] then
    print(("Error Player Is Already in the GPlayerList Table."))
    return
  end

  GPlayerList[source] = playerData

  print(json.encode(GPlayerList))
end)

AddEventHandler("playerDropped", function(reason)
  if source <= 0 then
    print('[VAdmin] (Server) Error 44')
    return
  end

  GPlayerList[source] = nil
  print(json.encode(GPlayerList))
  print(("Removed Player: %s (ID - %s)"):format(GetPlayerName(source), source))
end)


SetTimeout(5000, function()
  CreateThread(function()
    local Players = GetPlayers()
    for i = 1, #Players do
      local player = Players[i]
      if GPlayerList[player] then
        print(("VAdmin (Server) [64] Error Player Is Already in the GPlayerList Table."))
        return
      end
      GPlayerList[player] = {
        Name = GetPlayerName(player),
        ID = player,
        Identifiers = GetPlayerIdentifiersWithoutIP(player),
        HWIDS = GetPlayerTokens(player),
      }
    end

    print("[VAdmin] (Line: 15) (Server)")
  end)
end)

lib.callback.register('vadmin:plist', function(source)
  return GPlayerList
end)

I’ve been trying to go at it for a little bit, but i still don’t understand, yes there is other ways to do this that i know and understand but this is better on the server itself and is less demanding, but the issue is that once the player leaves it duplicates in the UI/Table.

Leave a Comment