How to check how many of something are in a folder

so basically when I made a pet equip script that seems to work fine but the problem is that it lets me equip the same exact pet twice and i found a solution where i have to give each of the pets a special id but i don’t know how i would

when i would click a picture in the inventory ui it would show in the “petinfo” section and when i press equip i made it so it clones the pet. i want to make it so that each of the pets have a special id as their “value” and the name of the value the pets name (im using a string value) so that when i select the one from the inventory section and i press equip it will equip that specific pet

so the most important thing is, how do i check how many things in a folder have the same name

ex:if there are 5 doggies in the folder than the fifths value is “5” etc

code

script.Parent.MouseButton1Down:Connect(function()
	local petName = script.Parent.Parent.petName.Text
	
	if game.ReplicatedStorage.Assets.Pets[petName] then
		local petModel = game.ReplicatedStorage.Assets.Pets[petName]:Clone()
		local positions = {
                        —not done
			["1"] = 1,1,0,
			["2"] = 2,1,0
			}
		
		local amountEquipped = #game.Players.LocalPlayer.EquippedPets:GetChildren()
		if amountEquipped < 5 and petModel.Equipped.Value == false then
			petModel.Equipped.Value = true
			petModel.Parent = game.Players.LocalPlayer.Character
			petModel:SetPrimaryPartCFrame(game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame)
			
			local attachChar = Instance.new("Attachment")
			attachChar.Visible = false
			attachChar.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
			attachChar.Position = Vector3.new(positions[amountEquipped + 1]) + petModel.PrimaryPart.Size
			
			local attachPet = Instance.new("Attachment")
			attachPet.Visible = false
			attachPet.Parent = petModel.PrimaryPart
			
			local alignPos = Instance.new("AlignPosition")
			alignPos.MaxForce = 25000
			alignPos.Attachment0 = attachPet
			alignPos.Attachment1 = attachChar
			alignPos.Responsiveness = 10
			alignPos.Parent = petModel
			
			local alignOr = Instance.new("AlignOrientation", petModel)
			alignOr.MaxTorque = 25000
			alignOr.Attachment0 = attachPet
			alignOr.Attachment1 = attachChar
			alignOr.Responsiveness = 10
			
			local newPet = Instance.new("StringValue", game.Players.LocalPlayer.EquippedPets)
			newPet.Name = petName
		end
	else
		print("No text or no pet found")
	end
end)
3 Likes
local folder = -- Folder with them

local nameToFind = -- The name of the thing you're counting (as a string)
local count -- Count of how many there are

for i, v in pairs(folder:Descendants()) do
    if v.Name == nameToFind then
        count += 1
    end
end

EDIT: Changed folder:GetChildren() to folder:GetDescendants, because the latter goes through every descendant of the folder, instead of just the direct children. And also, I want to clarify you need to fill in nameToFind with the name you’re trying to count the number of. And look at my later post if you’re trying to count the number of a certain type of object, like a BasePart.

6 Likes

i dont understand what you mean by #folder:GetChildren() , that would just be looping through the number of children

2 Likes

This code add 1 to the count value everytime the loops comes across a new object

2 Likes

Simple! I typed on an iPad, so expect syntax errors…

local yourFolder = some.folder.of.yours
local result = {}
for index, value in pais(yourFolder) do
    if value.Name == “something” then -- or check for some other property
        result[#result + 1] = value
    end
end

And, for how many…

local howMany = #result
2 Likes

Mine? It shouldn’t do that, as I placed a comment in the code for the variable nameToFind for OP to set to whatever name he’s looking for. If OP wants to find a certain type of object, then he can use this instead:

local folder = -- Folder with them

local count -- Count of how many there are

for i, v in pairs(folder:GetDescendants()) do
    if v:IsA() then -- Whatever type (like BasePart) you're looking to count the number of
        count += 1
    end
end
5 Likes

It’s simple, look:

local count = 0
for _,v in pairs(workspace:GetChildren()) do
	count = count + 1
end
print(count)