[HELP] How To Change Table Inside Table Key?

I Making A Pet System And I Searching How To Change Table In Table Index ?

My Code Output:
{
[1] =
{
[1] = “Hero”
[2] = 34096898-54323215-234543
[3] = true
}
}

My Want Code OutPut:
{
[“Hero”] =
{
[“PetID”] = 34096898-54323215-234543
[“Equipped”] = true
}
}

My Code:
table.insert(pets,{v.Name,{v.PetID.Value,v.EquipValue.Value})

You need to directly index it into the table, Like this: Pets["PetName"]
By using table.insert(), It will only add indexes using numbers and not strings, So that’s why you need to directly index it. That’s what an example code about it would look like:

local Pets = {
	["Name"] = {
		Name = "Hero",
		PetID = "34096898-54323215-234543",
		Equipped = true
	}
}

print(Pets.Name) --> Whole table
print(Pets.Name.PetID) --> 34096898-54323215-234543
print(Pets.Name.Equipped) --> true

how so example :

table.insert(pets,“Name”)

table.insert(pets[“Name”],{[“PetID”] = 534654, [“Equip”] = true})

Like i said, You can’t use table.insert() with strings… You need to do something like this:

Pets["NameOfPet"] = {
	Name = "PetName",
	PetID = "19834-325436-57452",
	Equipped = true,
}

how so how i can make I already asked this?

I already gave you an example of how you would do it. You simply need to do Pets["Name"] (Replace name with the Name of the Pet) and make it a table with all the Properties that you want.

Pets["Name"] = {
	["Name"] = "Hero",
	["PetID"] = "348945-3466-745745",
	["Equipped"] = true,
}

oh okay my example code:

table.insert(pets,v.Name)

pets[v.Name] = {[“Name”] = “Hero”,[“PetID”] = “348945-3466-745745”, [“Equipped”] = true,}

Yes, That’s how you would do it. You can also remove table.insert(pets, v.Name) (As when you declared it on the line below it’ll automatically insert it)

okay i test in my pet system thanks

not working code

local DTS = game:GetService("DataStoreService")
local petData = DTS:GetDataStore("petdatastpre")

game.Players.PlayerAdded:Connect(function(plr)
	local petsFolder = Instance.new("Folder",plr)
	petsFolder.Name = "PetsFolder"
	
	local success,errorMessage = pcall(function()
		petData:GetAsync(plr.UserId)
	end)
	
	if success then
		print(petData:GetAsync(plr.UserId))
	else
		print("u no have data")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if plr:FindFirstChild("PetsFolder") then
		local pets = {}
		
		for i, v in pairs(plr["PetsFolder"]:GetChildren()) do
			if v:IsA("Folder") then
				if v:FindFirstChild("PetID") and v:FindFirstChild("EquipValue") then
					table.insert(pets,v.Name)
					
					pets[v.Name] = 
						{
							["PetID"] = v.PetID.Value,
							["EquipValue"] = v.EquipValue.Value
						}
				else
					print("Error Saving Pet")
				end
				else
			end
		end
		
		petData:SetAsync(plr.UserId,pets)
	else
		print("Error")
	end
end)

image

Try changing your Player Removing Code to this:

game.Players.PlayerRemoving:Connect(function(plr)
	if plr:FindFirstChild("PetsFolder") then
		local pets = {}

		for i, v in pairs(plr["PetsFolder"]:GetChildren()) do
			if v:IsA("Folder") then
				if v:FindFirstChild("PetID") and v:FindFirstChild("EquipValue") then
					pets[v.Name] = {
						["PetID"] = v.PetID.Value,
						["EquipValue"] = v.EquipValue.Value
					}
				else
					print("Error Saving Pet")
				end
			else
			end
		end

		petData:SetAsync(plr.UserId,pets)
	else
		print("Error")
	end
end)

image
thankss