How to find two instances with the same name

repeat wait()
    -- do stuff here
until
    -- check if there are two instances with the same name

I don’t know what to put after “until.”
I’m trying to check if there are two instances with the same name.

2 Likes

Can you give more context? Are you trying to find two instances with the same on one parent or on different parents?

1 Like

I’m trying to check if there are two instances with the same name with the same parent

local parent = path to parent
local Instances = {}
local Name = "Hello"
for _ , v in ipairs(parent:GetChildren()) do
    if v.Name == Name then
       table.insert(Instances,v)
   end
end

This would add all children with the given name, you could limit it with an easy if condition

How can I check the amount of instances in a table?

1 Like

Where do the instances belong to?

local tableOfDescendants = {}

for _, v in pairs(workspace:GetDescendants()) do
	if tableOfDescendants[v.Name] then
		tableOfDescendants[v.Name] += 1
	else
		tableOfDescendants[v.Name] = 1
	end
end

for i, v in pairs(tableOfDescendants) do
	print("The instance of the name "..i.." belongs to "..v.." instances.")
end

This will work for all instances which are descendants of the workspace.

1 Like

You can use #Array to get the length of the array

local parent = path to parent
local Instances = {}
local Name = "Hello"
for _ , v in ipairs(parent:GetChildren()) do
    if v.Name == Name then
       table.insert(Instances,v)
       if #Instance >= 2 then break end
   end
end

You could do this to limit it with two instances.

local table = {}

repeat wait()
    -- do stuff here
    for i,v in pairs(path:GetChildren()) do
        if v.Name == "something" then
            if not table.find(table, v) then
                table.insert(table, v)
            end
        end
    end
until
    #table== 2

This works.

That only gets the occurrence of a single named instance, if you want to get the occurrence count for all uniquely named descendants of a path then you will need to use my writeup.

“with the same name with the same parent”

In future specify that you are looking for two or more instances of a specified name as opposed to two or more instances of any name.

The table global is being overwritten here, change the table variable name.