DataStore doesnt save values inside tables

which I am currently using with table.insert. I was saving the files I got from the player inventory into this table as UTF-8. but I noticed that only their names remain in the table. “PetHP” comes as 0,nil. what is the reason of this? How can I fix? Thank you very much for your help :slight_smile: I’m posting screenshotsimage
this one gets the player’s folder and inserts them inside to data table
image

Note : The only error is in the datastore, “PetHP” remains at the correct value as long as I don’t get the data from the datastore.

here is the full script.

local pl,wk,rn,db,prt,ds,debounce,ht = game:GetService("Players"),game:GetService("Workspace"),game:GetService("RunService"),game:GetService("Debris"),script.Parent,game:GetService("DataStoreService"),true,game:GetService("HttpService")
local dts = ds:GetDataStore("RaeStore")
pl.PlayerAdded:Connect(function(p)
	local petfolder = Instance.new("Folder",p)
	petfolder.Name = "PetFolder"
	local sc,ms = pcall(function()
		return dts:GetAsync("Pets"..p.UserId)
	end)
	if sc and ms then
		for i,v in ipairs(ms.Pets) do
			local newpetfl = Instance.new("Folder",petfolder)
			newpetfl.Name = "Pet"
			local newhealth = Instance.new("NumberValue",newpetfl)
			newhealth.Value = v.PetHP
			newhealth.Name = "PetHP"
			print(v)
		end 
	else
		warn(tostring(ms))
	end
end)
pl.PlayerRemoving:Connect(function(p)
	local datats = {
		["Pets"] = {
			
		}
	}
	local petfolder = p:FindFirstChild("PetFolder")
	for i,v in ipairs(petfolder:GetChildren()) do
		if v:IsA("Folder") and string.find(v.Name,"Pet") then
			local pethealth,namealgorithm = v:FindFirstChild("PetHP"),v.Name..i
			local newpetfoldertosave = {
				[tostring(namealgorithm)] = {
					["PetHP"] = pethealth.Value
				}
			}
			table.insert(datats.Pets,i,newpetfoldertosave)
		end
	end
	local sc,ms = pcall(function()
		return dts:SetAsync("Pets"..p.UserId,datats)
	end)
	if sc then	
	else
		warn(tostring(ms))
	end
end)
prt.Touched:Connect(function(h)
	local c = h.Parent
	if c and debounce then
		local hm,p = c:FindFirstChild("Humanoid"),pl:GetPlayerFromCharacter(c)
		if hm and p then
			debounce = false
			local petfolder = p:FindFirstChild("PetFolder")
			local newpet,hp = Instance.new("Folder",petfolder),math.random(1,100)
			newpet.Name = "Pet1"
			petfolder.Name = "PetFolder"
			local newpethealth = Instance.new("NumberValue",newpet)
			newpethealth.Name = "PetHP"
			newpethealth.Value = hp
			wait(1)
			debounce = true
		end
	end
end)

Edit : thats a part (im just testing it)

table.insert(datats.Pets,i,newpetfoldertosave) should be table.insert(dstats.Pets,newpetfoldertosave)

You don’t want to insert at a specified index. If you do, your array won’t be ordered and it won’t be able to save to json. Everything after the first null value will be dropped.

sadly didnt worked. it still returns nil.

Do you print the Health value before you set it? So you can debug.

Datastore table advice

I recommend shortening the strings you place in your datastore better yet just your acronyms you know you will understand/remember

since your inserting the pet table you could just

local NewPetFolderToSave = {
HP = pethealth.Value,
Id = tostring(namealgorithm)
}

thank you very much :slight_smile: but i found my mistake myself. I thought I was using a table again. but actually this is a dictionary and it is in the dictionary
We couldn’t call the PetHP value as dictionary.PetHP. I did this with a for loop and synced the values. actually there was no error in the datastore. it’s just my fault :sweat_smile: