Why is my array adding keys when it is not supossed too?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to add a pet array into the main array but for some reason it automatically adds a key after I evolve one of my pets.

before I evolve one of my pets and add a pet
OwnedPets = {{petinfo}, {petinfo}}

after I evolve one of my pets and add a pet
OwnedPets = {"6":{petinfo}, "7":{petinfo}}

  1. What is the issue? Include screenshots / videos if possible!

The code automatically adds a key when it is a numerical array.

here is my main add pet function code

function PetService:AddPet(player, petName)
	local NewTable = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("PetsOwned").Value)
	table.insert(NewTable, {Name = petName, Evolution = 1, Id = HttpService:GenerateGUID(false)})
	
	print("added pet, table should look like")
	
	for k,v in pairs(NewTable) do
		print(k)
		print(v)
	end
	
	player:WaitForChild("HiddenStats"):WaitForChild("PetsOwned").Value = HttpService:JSONEncode(NewTable)
end

here is the full code if you guys need it


local HttpService = game:GetService("HttpService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local PetStore = game:GetService("DataStoreService"):GetDataStore("MainStore")

local MarketPlaceService = game:GetService("MarketplaceService")

local Pets = ServerStorage:WaitForChild("Pets")

local closing = false

local PetService = {}

local Chances = {
	Egg1 = {
		["Epic"] = 10; --10
		["Rare"] = 20; --20
		["Uncommon"] = 30; --30
		["Common"] = 40; --40
	};
	
	Egg2 = {
		["Legendary"] = 10; --10
		["Epic"] = 20; --20
		["Rare"] = 30; --30
		["Uncommon"] = 40; --40
	};

	Egg3 = {
		["Mythical"] = 10;
		["Legendary"] = 20;
		["Epic"] = 30;
		["Rare"] = 40;
	};

	Egg4 = {
		["Shadow"] = 10;
		["Mythical"] = 20;
		["Legendary"] = 30;
		["Epic"] = 40;
	};

	Egg5 = {
		["Reverant"] = 5;
		["Shadow"] = 10;
		["Mythical"] = 20;
		["Legendary"] = 65;
	};

	Egg6 = {
		["Demonic"] = 5;
		["Reverant"] = 10;
		["Shadow"] = 20;
		["Mythical"] = 65;
	};

	Egg7 = {
		["Ultimate"] = 1;
		["Demonic"] = 5;
		["Reverant"] = 35;
		["Shadow"] = 59;
	};

	Egg8 = {
		["Neon"] = 0.1;
		["Ultimate"] = 10;
		["Demonic"] = 30;
		["Reverant"] = 59.9;
	};
}

local LevelText = {
	[1] = "Basic";
	[1.5] = "Shiny";
	[2] = "Golden";
}

local PetBonus = {
	Bear = {Clicks = 1.5, Gems = 1.10};
	Rabbit = {Clicks = 2, Gems = 1.25};
	Cow = {Clicks = 2.5, Gems = 1.5};
	Mouse = {Clicks = 3, Gems = 2};
	Koala = {Clicks = 5, Gems = 2.45};
	Cat = {Clicks = 6, Gems = 2.8};
	Deer = {Clicks = 6.5, Gems = 3.14};
	Dragon = {Clicks = 8, Gems = 3.49};
	Shark = {Clicks = 10, Gems = 3.83};
	["Angler Fish"] = {Clicks = 12, Gems = 4.18};
	["Aqua Dragon"] = {Clicks = 14, Gems = 4.52};
	["Forgotten Kraken"] = {Clicks = 15, Gems = 4.87};
	["Ice Golem"] =  {Clicks = 16, Gems = 5.21};
	["Jellyfish"] =  {Clicks = 20, Gems = 5.56};
	["Octopus Lord"] = {Clicks = 22, Gems = 5.90};
	["Aqua Lord"] = {Clicks = 25, Gems = 6.25};
	["Desert Spider"] = {Clicks = 30, Gems = 6.59};
	["Spikey Balloon"] = {Clicks = 32, Gems = 6.94};
	["Electric Spider"] = {Clicks = 35, Gems = 7.28};
	["Builder"] = {Clicks = 40, Gems = 7.63};
	["Golden Cat"] = {Clicks = 42, Gems = 7.97};
	["OctoHex"] = {Clicks = 45, Gems = 8.32};
	["Boom"] = {Clicks = 48, Gems = 6.66};
	["Oasis Lord"] = {Clicks = 50, Gems = 9.01};
	["Fireball"] = {Clicks = 55, Gems = 6.35};
	["Hellish Golem"] = {Clicks = 60, Gems = 9.70};
	["Lil Demon"] = {Clicks = 65, Gems = 10.04};
	["Demon"] = {Clicks = 70, Gems = 10.39};
	["Lava Beast"] = {Clicks = 80, Gems = 10.73};
	["Demonic Dominus"] = {Clicks = 85, Gems = 11.08};
	["Demonic Destroyer"] = {Clicks = 95, Gems = 11.42};
	["Ultimus"] = {Clicks = 100, Gems = 11.77};
}

game.Players.PlayerAdded:Connect(function(player)
	PetService:AddPet(player, "Bear")
	PetService:AddPet(player, "Bear")
	PetService:AddPet(player, "Bear")	
	PetService:AddPet(player, "Bear")
	PetService:AddPet(player, "Bear")
end)

function PetService:AddPet(player, petName)
	local NewTable = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("PetsOwned").Value)
	table.insert(NewTable, {Name = petName, Evolution = 1, Id = HttpService:GenerateGUID(false)})
	
	print("added pet, table should look like")
	
	for k,v in pairs(NewTable) do
		print(k)
		print(v)
	end
	
	player:WaitForChild("HiddenStats"):WaitForChild("PetsOwned").Value = HttpService:JSONEncode(NewTable)
end

function PetService:RemovePet(player, PetInfoTable)
	print("remove is called")
	local NewTable = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("PetsOwned").Value)
	local Slots = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value)
			
	for k,v in pairs(NewTable) do
		if v.Id == PetInfoTable.Id then
			NewTable[k] = nil
		end
	end
	
	for k,v in ipairs(Slots) do
		if v[3] == PetInfoTable[3] then
			PetService:RemovePetFromSlot(player, k)
		end
	end
		
	player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value = HttpService:JSONEncode(Slots)
	player:WaitForChild("HiddenStats"):WaitForChild("PetsOwned").Value = HttpService:JSONEncode(NewTable)
end

function PetService:AddPetToSlot(player, PetTable, slot)
	print("add pet to slot called")
	if player.Character.PetsFolder:FindFirstChild(PetTable[3]) == nil then
		local Slots = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value)
			
		Slots[slot] = PetTable
		player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value = HttpService:JSONEncode(Slots)

		local clone = Pets:WaitForChild(PetTable[1]):Clone()
		clone.Parent = player.Character.PetsFolder
		clone.Evolution.Value = PetTable[2]
		clone.Name = PetTable[3]
	
		local BP = Instance.new("BodyPosition")	
		BP.Parent = clone.HumanoidRootPart
		BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		
		local BG = Instance.new("BodyGyro")
		BG.Parent = clone.HumanoidRootPart
		BG.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	
		local UI = ServerStorage:WaitForChild("LevelUI"):Clone()
		UI.Parent = clone.HumanoidRootPart
		UI.Frame.TextLabel.Text = LevelText[PetTable[2]]
	end

	return true
end

function PetService:RemovePetFromSlot(player, slot)
	print("remove pet from slot called]")
	local Slots = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value)

	local petFolder = player.Character.PetsFolder
	local pet = false
	
	for i,v in ipairs(Slots) do
		if i == slot then
			pet = petFolder:FindFirstChild(v[3])
			break
		end
	end

	if pet ~= false then
		pet:WaitForChild("HumanoidRootPart").BodyPosition:Destroy()
		wait()
		pet:Destroy()
	
		Slots[slot] = {false, 0, 0}
	end
	
	for k,v in ipairs(Slots) do
		if Slots[k + 1] ~= nil then
			if Slots[k][1] == false and Slots[k + 1][1] ~= false then
				Slots[k] = Slots[k + 1]
				
				Slots[k + 1] = {false, 0, 0}
			end
		end
	end
	
	player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value = HttpService:JSONEncode(Slots)

	wait()

	return true
end

function PetService:GetOccupiedSlots(player)
	local Slots = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):waitForChild("CurrentSlots").Value)
	
	local Occupied = 0

	for _,v in ipairs(Slots) do
		if v[1] ~= false then
			Occupied = Occupied + 1
		end
	end

	return Occupied
end

function PetService:CreatePetPercentage(player, egg)
	
	local rand = math.random(1,100)

	local counter = 0

	for rarity, weight in pairs(Chances[egg]) do
		counter = counter + weight
		if rand <= counter then
			return rarity
		end
	end
end

function PetService:GetPetBonus(player, Type)
	local Slots = HttpService:JSONDecode(player:WaitForChild("HiddenStats"):WaitForChild("CurrentSlots").Value)

	if Type == "Clicks" then
		local clicks = 1

		for _,v in ipairs(Slots) do
			if v[1] ~= false and v[1] ~= nil then	
				clicks = clicks + (PetBonus[v[1]].Clicks * v[2])
			end
		end

		return clicks
	elseif Type == "Gems" then
		local gems = 1

		for _,v in ipairs(Slots) do
			if v[1] ~= false then
				gems = gems + PetBonus[v[1]].Gems
			end
		end

		return gems
	end
end


MarketPlaceService.ProcessReceipt = function(reciptInfo)
	local player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)

	if reciptInfo.ProductId == 10918407 then
		player:WaitForChild("HiddenStats").LocalPetStorage.Value = player:WaitForChild("HiddenStats").LocalPetStorage.Value + 25		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 10918396 then
		player:WaitForChild("HiddenStats").LocalPetStorage.Value = player:WaitForChild("HiddenStats").LocalPetStorage.Value + 2	
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 10918403 then
		player:WaitForChild("HiddenStats").LocalPetStorage.Value = player:WaitForChild("HiddenStats").LocalPetStorage.Value + 5	
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end


	if reciptInfo.ProductId == 10918411 then
		player:WaitForChild("HiddenStats").LocalPetInventory.Value = player:WaitForChild("HiddenStats").LocalPetInventory.Value + 50
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 10918415 then
		player:WaitForChild("HiddenStats").LocalPetInventory.Value = player:WaitForChild("HiddenStats").LocalPetInventory.Value + 100
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 10918482 then
		player:WaitForChild("HiddenStats").LocalPetInventory.Value = player:WaitForChild("HiddenStats").LocalPetInventory.Value + 250
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 1060066610 then
		player:WaitForChild("leaderstats").Clicks.Value = player:WaitForChild("leaderstats").Clicks.Value + 500000
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 1060067257 then
		player:WaitForChild("leaderstats").Clicks.Value = player:WaitForChild("leaderstats").Clicks.Value + 5000000
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 1060885750 then
		player:WaitForChild("leaderstats").Gems.Value = player:WaitForChild("leaderstats").Gems.Value + 5000000
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 1060885961 then
		player:WaitForChild("leaderstats").Gems.Value = player:WaitForChild("leaderstats").Gems.Value + 50000000
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 1061109485 then
		spawn(function()
			player:WaitForChild("ClickBoost").Value = true
			wait(900)
			player:WaitForChild("ClickBoost").Value = false
		end)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	if reciptInfo.ProductId == 1061110109 then
		spawn(function()
			player:WaitForChild("GemBoost").Value = true
			wait(900)
			player:WaitForChild("GemBoost").Value = false
		end)	
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

ReplicatedStorage.GetPetBonusTable.OnServerInvoke = function(player)
	wait(.5)
	return PetBonus
end


return PetService

have you tried using OOP to treat them as properties?

I am not rewriting my whole pet system

Not really, your just changing a few things in your paradigm. Especially since it functional programming it should be an easy switch

the guy who hired me wants this game out next week, if it was any other situation I would but time is key

I don’t know if this will help, but since you are using “PetsOwned” as an array, it looks like you are adding values with table.insert in the “AddPet” function but I noticed you are removing them by just setting their value to nil in the “RemovePet” function, which would result in gaps being created in your array.

What I mean is if you were to have an array with say, three items, and you set array[2] = nil, then Lua would think that the array now has only one item.

Also, have you tried printing the encoded JSON strings for PetsOwned before and after to verify that the problem’s source is within the AddPet function?

I dont know any other way to remove it from the array though without using nil as table.remove does nothing in this case

So the keys were already changed from numbers to strings before AddPet gets called?

soI just tested it and yes the letter keys get added in the add pet function after I evolve a pet

so I just checked it and the keys are are added after the first add pet call