For i, v in pairs returning duplicates? How to avoid?

  1. What do you want to achieve?
    I want my for i, v in pairs to return the only items in the list

  2. What is the issue?
    I want to know why my for i, v in pairs loop returns multiples of my pets

  3. What solutions have you tried so far?
    Unsure why its happening in the first place so I’m unsure how to search exactly or word it.

===================One Loop Method===========================
	for i = 1, #PetInventory do
		local pet = PetInventory[i]
			print(i, PetInventory[i].Name, PetInventory[i].Value)
==========The other=====================

       for i , v in pairs(PetInventory:GetChildren()) do
         print(i, v)

This is the output

1 Bunny 1
2 Bunny 2
3 Bee 3
4 Cat 4
1 Bunny 1
2 Bunny 2
3 Bee 3
4 Cat 4
1 Bunny 1
2 Bunny 2
3 Bee 3
4 Cat 4
1 Bunny 1
2 Bunny 2
3 Bee 3
4 Cat 4

Though I have these many pets.

image

3 Likes

Are they both returning doubles? If so, is this loop that prints the pets inside of another loop? Have you tried using ipairs instead of pairs?

I think it’s because the pairs loop is nested in the numeric loop.

this is not inside another loop. I’ve tried these variations in other scripts housed elsewhere and the prints return the same.

This is not the actual script just the different ways I ran the loop

1 Like

Alright, and you’re sure no other script could be cloning them? Could you just check at runtime?

Also, when is this code ran? Is it a .Touched event? That usually runs like 10 times for a single touch.

Just print the number of children in pet inventory for me. → print(#PetInventory) it should print as 4, if they are only 4 pets in the inventory. And if not, then we can narrow down the issue as it would seem the pets are getting duplicated.

Also can you include what you have “PetInventory” set to (What the variable is)

1 Like

I suppose it must be going on somewhere, I’ll search my game with ctrl,shift, f and find for loops that have to do with my pets.

This print runs at the start of the game, I’m trying to cross reference a pet ID and the slot the pet is sitting in for a sell function so it’s only firing once.

1 Like

Hmm, alright. Let me know about the values cloning thing.

PetInventory is set to player, every player has a PetInventory

for i = 1, #PetInventory do
	local pet = PetInventory[i]
		print(#PetInventory)

Ran your code and got 4(x16)

1 Like

Hmm, I mean it isn’t that script. (At least I don’t think) So I think you have ran the code multiple times within another script.

local function CreateFakeFolder(Player)
	local PetInventory = Instance.new("Folder", Player)
	PetInventory.Name = "PetInventory"

	local Bee = Instance.new("BoolValue", PetInventory)
	Bee.Name = "Bee"

	local Bunny = Instance.new("BoolValue", PetInventory)
	Bunny.Name = "Bunny"

	local Bunny = Instance.new("BoolValue", PetInventory)
	Bunny.Name = "Bunny"

	local Cat = Instance.new("BoolValue", PetInventory)
	Cat.Name = "Cat"
end

local function SortPetInventory(Player)
	local PetInventory = Player:WaitForChild("PetInventory")
	for i , v in pairs(PetInventory:GetChildren()) do
		print(v)
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	CreateFakeFolder(Player)
	wait()
	SortPetInventory(Player)
end)

I just ran that in studio, to create a replica of your folder and it works fine.

1 Like

Wow ty for doing all that I’ll dig through my other scripts and find the culprit then Thank both of you very much!

1 Like