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.