Inserting a table on a TTT entity

Hi everyone,

there’s an item available on the GMod workshop called “TTT Portable Tester”. Basically, when the player uses it, it prints a message saying if the player is an innocent/detective or a traitor. Problem is, the same player can use it again and again, and since it is destroyed after 3 uses, a single player can destroy it by pressing E three times.

I tried making it so the player can’t use it more than 1 time per round. For that, I tried creating a table that stores the player’s name and a function that checks if the player name is already in the table. If the player is already in the table, a message will display saying the player already got tested; else, he will activate the portable tester. Here is the shared.lua file of the entity, already modified by my table verifier function (called “table_contains”), the creation of a new table (called “tested”) and the conditions in which the tester can be used or not (under “function ENT:Use(ply)”):


if SERVER then 
AddCSLuaFile("shared.lua")
 end
 
if CLIENT then
   ENT.Icon = "VGUI/ttt/icon_health"
   ENT.PrintName = "Portable Tester"

   ENT.TargetIDHint = {
      name = "Portable Tester",
      hint = "E to use the tester!",
   };

end

ENT.Type = "anim"
ENT.Health = 50
ENT.Model = Model("models/props/cs_militia/microwave01.mdl")
ENT.CanUseKey = true
ENT.CanHavePrints = true
local uses = 0
AccessorFunc(ENT, "Placer", "Placer")

function table_contains(table, element)
	for k, v in pairs(table) do
		if v == element then
			return true
		end
	end
	return false
end

tested = {}

function ENT:Initialize()
   self:SetModel(self.Model)

   self:PhysicsInit(SOLID_VPHYSICS)
   self:SetMoveType(MOVETYPE_VPHYSICS)
   self:SetSolid(SOLID_BBOX)

   local b = 32
   self:SetCollisionBounds(Vector(-b, -b, -b), Vector(b,b,b))

   self:SetCollisionGroup(COLLISION_GROUP_WEAPON)
   if SERVER then
      self:SetMaxHealth(200)

      local phys = self:GetPhysicsObject()
      if IsValid(phys) then
         phys:SetMass(5)
      end

      self:SetUseType(CONTINUOUS_USE)
   end
   self:SetHealth(200)

   self:SetColor(Color(255, 255, 255, 255))

   self:SetPlacer(nil)

   self.fingerprints = {}
end

function ENT:Use(ply)
local amount = 1
   if IsValid(ply) and table_contains(tested, ply:Nick()) == false and ply:GetRole() == ROLE_INNOCENT or ply:GetRole() == ROLE_DETECTIVE then
   self:SetColor(Color(0, 255, 0, 255))
   timer.Create( "custom timer", 3, 1, function()
   self:SetColor(Color(255, 255, 255, 255))
   PrintMessage( HUD_PRINTTALK, "Player " .. ply:Nick() .. " got tested and was an innocent/detective!" )
   uses = uses + 1
   table.insert(tested, ply:Nick())
   end)
   elseif ply:IsTraitor() and table_contains(tested, ply:Nick()) == false then
   self:SetColor(Color(255, 0, 0, 255))
   timer.Create( "custom timer2", 3, 1, function()
   self:SetColor(Color(255, 255, 255, 255))
   PrintMessage( HUD_PRINTTALK, "Player " .. ply:Nick() .. " got tested and was a traitor! KOS HIM!" )
   uses = uses + 1
   table.insert(tested, ply:Nick())
   end)
   elseif table_contains(tested, ply:Nick()) == true then
   PrintMessage( HUD_PRINTTALK, "Player " .. ply:Nick() .. "already got tested!" )

   end
end


function ENT:OnTakeDamage(dmginfo)
   if dmginfo:GetAttacker() == self:GetPlacer() then return end

   self:TakePhysicsDamage(dmginfo)

   self:SetHealth(self:Health() - dmginfo:GetDamage())

   local att = dmginfo:GetAttacker()
   if IsPlayer(att) then
      DamageLog(Format("%s damaged a portable tester for %d dmg",
                       att:Nick(), dmginfo:GetDamage()))
   end

   if self:Health() < 0 then
      self:Remove()
	  
      util.EquipmentDestroyed(self:GetPos())

      if IsValid(self:GetPlacer()) then
         LANG.Msg(self:GetPlacer(), "hstation_broken")
      end
   end
end

function ENT:Think()
if uses == 3 then
self:Remove()
util.EquipmentDestroyed(self:GetPos())
uses = 0
end
end

The above code isn’t changing anything from the original workshop file. The tester continues breaking after the same player uses it 3 times. I’m relatively new to coding, so I’m sorry if it’s just a simple mistake and I’m really stupid. Thanks in advance!