Seeking Coding Genius -- Need Help With Strange Problem!

So I am creating a pet system where you can buy multiple pets. When you leave the game I save the pet and data about it to the datastore. When you come back to the game then the code copy’s the animal from the list in ServerStorage, moves it to the players folder, and updates the data. The code works–but only if you have one animal. It breaks on the second animal and I don’t know why. Here is the code with explanation.

–I have received the data called “OwnedPets”. The data includes a set for each saved pet:
{Pet Type ID, Name, Level, Food Eaten, Individual pet ID,}


for i, v in pairs(OwnedPets) do
print(“1”)
local PetIdNum = OwnedPets[i][1] --the pet id number from storage
print(“2”)
local PetList = SavedPets:GetChildren() – the list of possible pets
print(“3”)
if PetIdNum == PetList[i].PetIdNum.Value then --will the code check every pet on the list here? Or do I need another loop? I tried a loop and it still broke so I changed it to this.
print(“4”)
local pet = PetList[i]
print(“5”)
local copy = pet:Clone() --copy pet
copy.Name = OwnedPets[i][2] --change name
copy.Level.Value = OwnedPets[i][3] --change some other values
copy.Poofs.Value = OwnedPets[i][4]
copy.ID.Value = OwnedPets[i][5]
copy.Head.Prompt:Destroy(). – delete the prompt to buy it
copy.Parent = player:WaitForChild(“Pets”). – parent to player folder
print(“6”)

When I run the code with two pets in the OwnedPets what I get is: 1,2,3,4,5,6,1,2,3. The code is breaking at 3 with the second petdata pack. Why? I tried a for loop over the PetList, but it still broke here. For some reason when the loop runs the second time, it is not finding PetIdNum == PetList[i].PetIdNum.Value.

Any idea why? How can I fix this?

If you want to run the code, then I can post all the relevant code but you will have to make some folders and objects to do it.

Okay, I have been testing this code and double checking everything. To code breaks down at print(“3”) on the second pet data pack both in studio and in game. I can confirm that the code is picking up the second data pack at and the break point, I know that:

–the code has the second data pack and has identified the correct id number
–the PetList of animals has not changed so the matching pet is in the list

But the if … then loop is NOT running the second time with identical information. What could cause that? I am at a total loss.

I changed the code to see if I could clone the first pet multiple times. Maybe the cloning was crashing it somehow? I can. The code from print(“4”) to end can run multiple times without a problem.

I will take literally any ideas that you have, however wacky.

here is the output I am getting:

10:10:12.520 ▼ {. -----FOUND BOTH ANIMALS IN DATASTORE
[1] = ▼ {
[1] = 1234,
[2] = “AdultPenguin1”,
[3] = 0,
[4] = 0,
[5] = 3.819061666663116e+23
},
[2] = ▼ {
[1] = 1234,
[2] = “AdultPenguin1”,
[3] = 0,
[4] = 0,
[5] = 4.71047528479941e+22
}
} - Server - Pets and Clothes Save Script:28
10:10:12.521 1 - Server - Pets and Clothes Save Script:32
10:10:12.521 ▼ {. ----TOOK FIRST ANIMAL
[1] = 1234,
[2] = “AdultPenguin1”,
[3] = 0,
[4] = 0,
[5] = 3.819061666663116e+23
} - Server - Pets and Clothes Save Script:33
10:10:12.521 2 - Server - Pets and Clothes Save Script:34
10:10:12.522 3 - Server - Pets and Clothes Save Script:36
10:10:12.522 ▼ {. --COMPLETE LIST OF POSSIBLE PETS
[1] = AdultPenguin1,
[2] = Blue Alien NPC,
[3] = Bubbles,
[4] = Developer,
[5] = Dog,
[6] = Fat Cat ,
[7] = NPC Bubbles,
[8] = PieMaster Puff,
[9] = PoPo Bot,
[10] = AdultPenguin
} - Server - Pets and Clothes Save Script:37
10:10:12.522 ▼ {. -----CONFIRM FIRST PET
[1] = 1234,
[2] = “AdultPenguin1”,
[3] = 0,
[4] = 0,
[5] = 3.819061666663116e+23
} - Server - Pets and Clothes Save Script:38
10:10:12.523 5 - Server - Pets and Clothes Save Script:43
10:10:12.526 6 - Server - Pets and Clothes Save Script:51
10:10:12.526 1 - Server - Pets and Clothes Save Script:32
10:10:12.526 ▼ {. ----SCRIPT RAN SECOND TIME AND FOUND SECOND PET
[1] = 1234,
[2] = “AdultPenguin1”,
[3] = 0,
[4] = 0,
[5] = 4.71047528479941e+22
} - Server - Pets and Clothes Save Script:33
10:10:12.526 2 - Server - Pets and Clothes Save Script:34
10:10:12.527 3 - Server - Pets and Clothes Save Script:36
10:10:12.527 :arrow_forward: {…} - Server - Pets and Clothes Save Script:37 —COMPLETE LIST OF PETS THE SAME
10:10:12.527 ▼ {. --CONFIRM THE SECOND ANIMAL WHICH DIFFERS ONLY IN PERSONAL ID CODE
[1] = 1234,
[2] = “AdultPenguin1”,
[3] = 0,
[4] = 0,
[5] = 4.71047528479941e+22
} - Server - Pets and Clothes Save Script:38

CRASH…

I would appreciate it if you format your code and the output using a code block:
image

3 Likes

… and for messages/outputs use > as prefix

2 Likes

ok! Thank you guys for looking at this! Here is the complete function. I moved an end to after the trigger remoteevent function.


local succ pcall(function()

local OwnedPets = PetsData:GetAsync(player.UserId.."Pets")
	print(OwnedPets)
	PetFolder:GetChildren()
	
	for i, v in pairs(OwnedPets) do
		print("1")
		print(OwnedPets[i])
		print("2")
		local PetList = SavedPets:GetChildren()
		print("3")
		print(PetList)
		print(OwnedPets[i])
		 
		if OwnedPets[i][1] == PetList[i].PetIdNum.Value then
			local pet = PetList[i]
		
				print("5")
				local copy = pet:Clone()
				copy.Name = OwnedPets[i][2]
				copy.Level.Value = OwnedPets[i][3]	
				copy.Poofs.Value =  OwnedPets[i][4]
				copy.ID.Value = OwnedPets[i][5]
				copy.Head.Prompt:Destroy()
				copy.Parent = player:WaitForChild("Pets")
				print("6")
	
	end	

	
	local PetsOwned = {}
print("7")
	for i,v in pairs(player.Pets:GetChildren()) do
		print("8")
		table.insert(PetsOwned,{v.PetIdNum.Value, v.Name, v.Level.Value, v.Poofs.Value, v.ID.Value})
		print("9")
		SendPetData:FireClient(player, PetsOwned)
	print("10")
		end
		end
end)

Okay, so it doesn’t loop twice right?. Have you checked all the pets in the table

I think you would need to loop through the available pet list also:

local OwnedPets = PetsData:GetAsync(player.UserId.."Pets")
	print(OwnedPets)
	PetFolder:GetChildren()
	
	for i, v in pairs(OwnedPets) do
		print("1")
		print(OwnedPets[i])
		print("2")
		local PetList = SavedPets:GetChildren()
		print("3")
		print(PetList)
		print(OwnedPets[i])
	
	for z,x in ipairs(PetList) do
		if OwnedPets[i][1] == x.PetIdNum.Value then
			local pet = PetList[z]
		
				print("5")
				local copy = pet:Clone()
				copy.Name = OwnedPets[i][2]
				copy.Level.Value = OwnedPets[i][3]	
				copy.Poofs.Value =  OwnedPets[i][4]
				copy.ID.Value = OwnedPets[i][5]
				copy.Head.Prompt:Destroy()
				copy.Parent = player:WaitForChild("Pets")
				print("6")
	
	  end	
    end
	
	local PetsOwned = {}
print("7")
	for i,v in pairs(player.Pets:GetChildren()) do
		print("8")
		table.insert(PetsOwned,{v.PetIdNum.Value, v.Name, v.Level.Value, v.Poofs.Value, v.ID.Value})
		print("9")
		SendPetData:FireClient(player, PetsOwned)
	print("10")
		end
		end
end)
1 Like