DataStoreService vs. HttpService:JSONEncode()

I’m currently working on a map sharing system, which requires me to store a table of data for every part in the map inside another table. It seems that DataStoreService has issues with storing things like this as it keeps throwing this error at me:

DataStoreService: CantStoreValue: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. API: SetAsync, Data Store: MapStore v.0

Is JSONEncode a better method that can handle things like this?

Here is my code:

local DataStore = game:GetService("DataStoreService"):GetDataStore("MapStore v.0")

wait(1)



function LoadTrack(track_id)
	local data
	local success, err = pcall(function()
		data = DataStore:GetAsync(track_id)
	end)
	if err then warn("Data didn't load") end
	
	if not data then print("Track doesn't exist") return end
	
	print(data)
end


function SaveTrack(track_id, mapTable)
	print("Saving current track to id "..track_id)
	local success, err = pcall(function()
		local ds = DataStore:SetAsync(track_id, mapTable)
	end)
	if err then warn("Data didn't save") end
end


game.ReplicatedStorage.Events.LoadorSaveMap.OnServerEvent:Connect(function(plr, track_id, loadvalue)
	if not tonumber(track_id) then warn("ID must be a number!") return end
	if loadvalue then LoadTrack(track_id) return end
	
	
	
	local TrackTable = {}
	
	
	for i,part in pairs(workspace.Map:GetChildren()) do
		
		local partInfo = {
			
			Pivot = part:GetPivot(),
			Name = part.Name
			
		}
		
		TrackTable[part.Name] = partInfo
	end
	
	
	SaveTrack(track_id, TrackTable)
end)

I believe you can only save values which represent JSON encodeable types, such as numbers, strings and other dictionaries. CFrames are not a valid datatype.

Would using tostring(Pivot) fix this issue?

Using tostring(part:GetPivot()) ended up fixing this issue. Thank you so much for helping!

1 Like

Not sure if CFrames can be converted into strings, but if that does work, you’d have to manually convert it into a CFrame when getting the Pivot back from the datastore.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.