Datastore only reading one data

Alright, i’ll get straight to the point.

Somehow, my datastore isn’t reading any second data. Only one data.
Lets say i created a bool value and i saved it using json encode. Then i make another and i do the same.

local function saveData(plr,code,rewardxd,val)
	local codeData = {}
	
	print(val.Value)
	codeData[code] ={Reward = rewardxd.Name, Code = code,Redeemed = val.Value}
	local ToSave = http:JSONEncode(codeData)
	DataStore:SetAsync(plr.UserId,ToSave)
end

Then we decode it when the player joins.
we loop through that decoded data and bam, when i try printing “v” it only prints one table.

local savedData = DataStore:GetAsync(plr.UserId)
	if savedData ~= nil then
		local Data = http:JSONDecode(savedData)

		task.wait()
		if Data ~= nil then
			for i,v in pairs(Data) do
				print(v) --only prints once

i dont know much about jsonDecode and encode so if you find any mistakes. Feel free to point it out.

Thanks!! :smiley:

1 Like

The problem is that you replace the old table. Have the save function like this:

local function saveData(plr,code,rewardxd,val)
	local codeData = DataStore:GetAsync(plr.UserId) or {}
	
	print(val.Value)
	codeData[code] ={Reward = rewardxd.Name, Code = code,Redeemed = val.Value}
	local ToSave = http:JSONEncode(codeData)
	DataStore:SetAsync(plr.UserId,ToSave)
end
1 Like
attempt to index string with 'eee'

Oh sorry, forgot you encoded it

local function saveData(plr,code,rewardxd,val)
	local codeData = http:JSONDecode(DataStore:GetAsync(plr.UserId)) or {}
	
	print(val.Value)
	codeData[code] ={Reward = rewardxd.Name, Code = code,Redeemed = val.Value}
	local ToSave = http:JSONEncode(codeData)
	DataStore:SetAsync(plr.UserId,ToSave)
end
1 Like