DataStore2 Update value is nil?

When I print purchaseHistory I get a table followed by it’s memory position, which is expected, but inside the update call, newValue/oldValue are nil. Any suggestions or ideas?

print("Finding cache", Cacheing.Cache[client.UserId].PurchaseHistory)
Cacheing.Cache[client.UserId].PurchaseHistory:Update(function(oldValue)
	local newValue = oldValue
	print("old", oldValue)
	print("new",newValue)
	table.insert(newValue,{os.time(), receiptInfo.CurrencySpent})
									
	return newValue
end)

There’s not enough code here to determine what the source of your problem is. The only thing I do know is that newValue is nil because oldValue is. What is the table of PurchaseHistory and what does the function Update perform?

PurchaseHistory is DS2(“PurchaseHistory”, player), a DS2 cache.

This was working perfectly fine up until about a day ago, and I haven’t changed anything.

If DataStore2 hasn’t cached the value of your purchasehistory datastore, the oldValue argument passed in the Update callback function will be nil. There’s an existing issue on Github for it and it’s mentioned in the API documentation.

Ensuring the value is cached before using the Update method solves the problem. Simply calling DataStore:Get with the default value argument should be sufficient.

local ds2 = require(game:GetService("ReplicatedStorage").ds2)

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local ph = ds2("ph", Player)
	local ph_value = ph:Get(0) --< remove this line and oldVal will be nil, even if data has been previously saved.
	
	ph:Update(function(oldVal)
		print(oldVal)
		return oldVal
	end)
end)
3 Likes

Alright, that was my guess. Thank you very much, this has been very helpful.

1 Like