Unable to store dictionary in datastore

how can i get it to save a dictionary or what alternatives could i try


		if success then
			print("connected player to database")
			if true then --not playerData then
				print("Assigning default data")
				playerData = {
					["Cash"] = 10,
					["TotalDisasters"] = 0,
					["SurvivedDisasters"] = 0,
					["TotalMaps"] = 0,
					["TotalDeaths"] = 0,
					["TotalCash"] = 10,
					["TotalJoins"] = 0,
					["JoinDate"] = os.date(),
					["JoinEpoch"] = os.time(),
					["Banned"] = false,
					["BannedReason"] = "No Reason Given",
					["BannedUntil"] = 0,
					["BannedUntilYear"] = 0,
					["BannedUntilMonth"] = 0,
					["BannedUntilDay"] = 0,
					["TotalBans"] = 0,
					["LastDate"] = os.date(),
					["LastEpoch"] = os.time(),
					["CurrentTitle"] = 0,
					["Inventory"] = {
					{
						Type = "Title",
						Name = "Alpha Tester",
						Rarity = "Legendary",
						Text = "Alpha Tester",
						Col = Color3.new(0.133333, 0.392157, 1)
					},
					{
						Type = "Item",
						Name = "Classic Sword",
						Rarity = "Common",
						Tool = ReplicatedStorage.Assets.Tools.ClassicSword,
					}
				}
				}
			end
			sessionData[newPlayer.UserId] = playerData
  20:40:53.368  DataStoreService: CantStoreValue: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. API: SetAsync, Data Store: data  -  Studio
  20:40:53.383  104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.  -  Server
function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		repeat 
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end) 	

			attempt += 1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5

		if success then
			print("Data saved for ", player.Name)
		else
			warn("Data unable to be saved for ", player.Name)
		end
	end
end
game.Players.PlayerRemoving:Connect(PlayerLeaving)

Use JSONEncode from HttpService to convert the dictionary into a string to save it, and use JSONDecode to convert the string into a dictionary.

1 Like

Values of type Color3 and Instance can’t be saved in a datastore, you need to figure out how to convert them to a string value and decode them back when you load them. For both of them, as long as you don’t care about the amount of stored information, you can simply use tostring to convert them to strings. Then to decode the color you can use the following function:

local function strToC3(str: string): Color3
	local r, g, b = str:split(", ")
	return Color3.new(r, g, b)
end

Decoding the Instance/Tool may be a bit trickier, so I suggest you figure out a way to keep minimal information instead of the entire path, so if for example, the path changes later on it won’t affect old player data(for example you can store the tool name, if all tool names are unique).

Im still confused on how to implement this…