Trouble with saving Tables

Hello Developers, I’m currently having a really bad time trying to save some Tables.

I’m basically trying to save some coins which have some properties (xp, level, nicknames, isEquipped).

If I have only one Coin in my inventory, everything will work & save fine.
However, if I have 2 Coins in my inventory, I will get a 104 Error (Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.)

If I try to save the same coin 2 times, it will work just fine.

Here’s the code:

local Data = {
	Inventory = {
		Coins = {};
	}:
};

for _, Item in pairs(Player.Inventory.Coins:GetChildren()) do
	local ResultEquipped
	if Player.ItemEquipped.Value == Item then
		ResultEquipped = true
		warn(ResultEquipped)
	else
		ResultEquipped = false
		warn(ResultEquipped)
	end
	
	local DataToInsert = {
		Id = Item.Name;
		Xp = Item.Xp.Value;
		Level = Item.Level.Value;
		Nickname = Item.Nickname.Value;
		Equipped = ResultEquipped;
	};
		
	table.insert(Data.Inventory.Coins, DataToInsert)
end

local success, errMsg = pcall(function()
	PlayerData:SetAsync(Data, UserId)
end)

Thanks! Help will be appreciated.

What do you mean with this? You can save any value in a data store as long as it can be encoded with JSON (which is now done automatically).

Your script is a bit desorganized, can you give more information?

It’s saving only booleans, numbers and strings, nothing else.

There isn’t too much stuff about it, idk what’s really causing it because as I said if I will add the same table like 2 times, everything will work fine, but if I add a different table it will just crash.

Instead of saving the table directly, save an encoded version like this:

local tableToSave = {example = 1;}
talbeToSave = game:GetService("HttpService"):JSONEncode(tableToSave)

You can’t save Vectors,Rays,Instance (etc)
You only can store Numbers,booleans,strings and tables.


Saving the position

local http = game:GetService("HttpService")
local part = workspace.Part
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("ObjectPosition")

local pos_x = part.Position.X
local pos_y = part.Position.Y
local pos_z = part.Position.Z

local objectTable = {Position = {X = x, Y = y, Z = z}}
objectTable = http:JSONEncode(objectTable)
datastore:UpdateAsync("table",function(oldValue)
     return objectTable
end)

Getting the position

local http = game:GetService("HttpService")
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("ObjectPosition")

local part = Instance.new("Part")
part.Parent = workspace

local data = datastore:GetAsync("table")
if not data then return end
data = http:JSONDecode(data)

local position = Vector3.new(data.Position.X,data.Position.Y,data.Position.Z)
part.Position = position
1 Like

This has nothing to do with it. I said my script only saved Numbers, Strings and Booleans and Tables.

Thats what i said, re-read the post.
Datastore only saves Numbers, strings, booleans and tables.
if you want to saving an instance or ray or vector then you have to serialize it.

Yes, but as I said, I don’t want to save any stuff like that.

Thats an example, and i think you are saving wrong table.
image
image
image

you are adding data to the wrong table

Thank you so much for your help.

That was it, you sometimes have to Encode the data and Decode it when you want to load.

That’s another table which has in it the table with the issue.

Anyways, this was fixed. Thanks to all trying to help!