Finding if two instances have the same name and changing the name of one

I have been trying to make a system for a train game I am working on. I have a system that generates a four-digit code to identify them.

local Code = math.random(1000,9999)

What I need is:

  • Find two instances with the same Name, not what they are
  • Leave the first one’s Name with the code already set
  • Generate a new code for the second one’s Name
local codes,dupes = {},{}

for i = 1,1000 do 
local generated = math.random(1000,9999)
if table.find(dupes,generated) then print(generated.." is a dupe. number: ".. i) return end
table.insert(codes,generated)
end


idk if this is what your looking for