How to check if there are two of Parts with the same name in the same folder?

Hello Roblox,

I want to make a script where to check if there are two parts with the same name in the same folder.
Please help me.

~Maini

1 Like
function check(folder)
	local found = {}
	for i,v in pairs(folder:GetDescendants()) do
		if v:IsA("BasePart") then
			if table.find(found,v.Name) then
				return true
			else
				table.insert(found,v.Name)
			end
		end
	end
	return false
end
3 Likes

This Code doesnt work. I mean with folder the backpack and with parts tools. Sorry XD

Then you simply change what type of object you are searching for. Currently the code is searching for “BasePart”, try changing it to “Tool” if you’re looking only for tools.

function check(Player)
	local found = {}
	for i,v in pairs(Player.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			if table.find(found,v.Name) then
				return true
			else
				table.insert(found,v.Name)
			end
		end
	end
	return false
end

But what does “table.find(found,v.Name)” do in the code?

It finds a name within the table, if a name within the table is the same name that we are searching for, then by logic we can deduce that there are multiple tools with the same name.

Checks in the table ‘found’ if anything with the value of the name of v (the current tool its reached while looping through the contents of the backpack) and returns true or nil

local Player = nil --define player here
local ToolsArray = {}

for _, Tool in ipairs(Player.Backpack:GetChildren()) do
	if Tool:IsA("Tool") then
		if ToolsArray[Tool.Name] then
			ToolsArray[Tool.Name] += 1
		else
			ToolsArray[Tool.Name] = 1
		end
	end
end

for ToolName, Quantity in pairs(ToolsArray) do
	print(Quantity.." copies of "..ToolName.." founds in the player's backpack.")
end