Issue with decoding saved tables from a datastore

Source code:

httpservice = game:GetService("HttpService")
datastoreservice = game:GetService("DataStoreService")
gamedata = datastoreservice:GetDataStore("gamedata")

game.Players.PlayerAdded:Connect(function(player)
	blocksvalue = nil
	success = pcall(function()
		blocksvalue = gamedata:GetAsync(player.UserId.."blocks")
	end)
	if success == true then
		if blocksvalue ~= nil then
			decoded = httpservice:JSONDecode(blocksvalue)
			for i,v in pairs(decoded) do
				if string.find(i,"GrassBlock") ~= nil then
					block = game.ServerStorage.GrassBlock:Clone()
					block.Position = v
					block.Parent = game.Workspace.Blocks
				elseif string.find(i,"DirtBlock") ~= nil then
					block = game.ServerStorage.DirtBlock:Clone()
					block.Position = v
					block.Parent = game.Workspace.Blocks
				elseif string.find(i,"StoneBlock") ~= nil then
					block = game.ServerStorage.StoneBlock:Clone()
					block.Position = v
					block.Parent = game.Workspace.Blocks
				elseif string.find(i,"BedRockBlock") ~= nil then
					block = game.ServerStorage.BedRockBlock:Clone()
					block.Position = v
					block.Parent = game.Workspace.Blocks
				elseif string.find(i,"OakLeafsBlock") ~= nil then
					block = game.ServerStorage.OakLeafsBlock:Clone()
					block.Position = v
					block.Parent = game.Workspace.Blocks
				elseif string.find(i,"OakWoodBlock") ~= nil then
					block = game.ServerStorage.OakWoodBlock:Clone()
					block.Position = v
					block.Parent = game.Workspace.Blocks
				end
			end
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	blockstable = {}
	inworldblocks = game.Workspace.Blocks:GetChildren()
	for i,v in pairs(inworldblocks) do
		blockstable[v.Name .. i] = v.Position
	end
	outworldblocks = game.ReplicatedStorage.Blocks:GetChildren()
	for i,v in pairs(outworldblocks) do
		blockstable[v.Name .. i] = v.Position
	end
	encoded = httpservice:JSONEncode(blockstable)
	gamedata:SetAsync(player.UserId.."blocks",encoded)
end)

The issue is when I decode the saved dictionary, it returns an empty table. Any help would be much appreciated :slight_smile:

(Its for a singleplayer minecraft game)

Vector3’s can’t be serialized, so you will have to write your own serializer.

Serializing a Vector3 is pretty easy, you can just save the xyz coordinates in an array.

Then here

you can do

block.Position = Vector3.new(table.unpack(v))

By the way it is completely redundant and is in fact bad practice to JSON-encode before saving to data stores, so don’t do that. In fact you can take even 2x more space, since you are encoding it twice and there would be more quotes/backslashes getting added.

1 Like

Thanks so much for the quick reply, I totally forgot about that! I’ll try that out later and update you on it :smiley: