How do I save tables?

I’m creating a inventory storing system for my game and need to store a table. I’m using basically the same script I would if I were storing strings or numbers, but I’m getting a strange error. The error I got will be down below, I’m not exactly sure what it means. I’ll also be putting an example of what I’m doing below. Yes I have searched for things related to this, but none of them worked for me.

dev_forums_savingTables

local t = {["Clockwork Shades"] = {Name = "Clockwork Shades", Rarity = "Rare"}}

game.Players.PlayerAdded:Connect(function(player)

local key = string.format("Player_%d", player.UserId)
local data = game.DataStoreService:GetDataStore("stuff"):GetAsync(key) or t

end)

game.Players.PlayerRemoving:Connect(function(player)

local key = string.format("Player_%d", player.UserId)
game.DataStoreService:GetDataStore("stuff"):SetAsync(key, t)

end)

https://www.utf8-chartable.de/

Your tables can’t save cause you are using invalid character into these arrays

Which character in the array is invalid?

Are you allowed to store arrays in data stores? That error sounds a bit inaccurate. I feel like it should say they only accept valid UTF-8 strings. Note: I’ve never used data stores before.

Edit: It says it right in the error

Cannot store Array in data store.

I’m pretty sure you can store arrays, before I made this post I saw a post where somebody saved a table:

Saving a Table in Datastore - Help and Feedback / Scripting Support - Roblox Developer Forum

Edit: Can you show the code where you are storing the data and show what the data is?

I’d assume behind the scenes when you supply a dictionary, it automatically knows what to do. It grabs the key and uses that as the key and then the value and uses that as a value for each key/value pair. If you try to save t, then it will grab the key “Clockwork Shades” and then grab the value which is an array. This would explain the error.

1 Like

I think only saving a table of the item’s asset ids may work better, I’ll try it right now and tell you if it works.

As i think, square brackets “[ ]” are invalid to hold a datastore with tables, try removing them and see if it works,

1 Like

You need to encode a table to store it in data stores. This turns a table into a string.

1 Like