103: Array is not allowed in data stores [Solved!]

I’m nub at Data Store so help me please. How can i save Array in Data Store? Because this:

    function savePlace(player)
    	local models = {}
    	local base = workspace.Slots:FindFirstChild(player.Name).PrimaryPart
    	for _, model in pairs(workspace.Slots:FindFirstChild(player.Name).Items:GetChildren()) do
    		local modelData = {}		
    		local primaryPart = model.PrimaryPart
    		local position = primaryPart.Position - base.Position
    		local direction = primaryPart.CFrame.lookVector
    		modelData.ModelName = model.Name
    		modelData.Position = {position.X, position.Y, position.Z, direction.X, direction.Y, direction.Z}
    		models[#models + 1] = modelData
    	end
    	slotDs:SetAsync(player.UserId, models)
    	print('done')
    end

Shows me 103: Array is not allowed in data stores. error.
How can i save this Array?

2 Likes

As far as i know, DataStoreService is mainly used to store numbers/ strings. I don’t think you can store objects, like parts.

As a side note: please make use of indentation, because that is much easier to read.

1 Like

I’m new at DevForum too, but thanks.

I don’t save parts, i saving part.Position, part.Name and part.CFrame.LookVector which adding in table.

Ah, my bad. I’m having a difficult time reading the script. Leaving some whitespace between the lines of code would also help, to understand it better. I’ll try the script out for myself and try to improve it.

1 Like

Okay.

If you want check full script then go here Saving parts/models via datastore? - #33 by Ultraw .
I used script from there as Base for my.

I rewrote your script and i’ve experienced the same error. The variant i made has indentation, which makes it easier to read.

Players = game:GetService("Players")

DataStoreService = game:GetService("DataStoreService")
MyDataStore = DataStoreService:GetDataStore("MyDataStore")

function SavePlace(Player)
	local Models = {}
	local Base = workspace.Slots:FindFirstChild(Player.Name)
	local PrimaryPart_Base = Base.PrimaryPart

	for _, Model in pairs(Base.Items:GetChildren()) do
		local ModelData = {}
		
		local PrimaryPart_Model = Model.PrimaryPart
		local ModelCFrame= PrimaryPart_Model.CFrame * PrimaryPart_Base.CFrame:Inverse()
		
		ModelData.ModelName = Model.Name
		ModelData.ModelCFrame = ModelCFrame
		
		table.insert(Models, ModelData)
	end
	MyDataStore:SetAsync(Player.UserId, Models)
	print("Done!")
end

Players.PlayerRemoving:Connect(SavePlace)

The error i got was:

Cannot store Array in data store. Data stores can only accept valid UTF-8 characters.

2 Likes

So i can’t save table in Data Store?

You can’t store cframes in datastores. Thats why ur getting an error.

1 Like

You mean my script or script by @THEsuperTERROR ?

No, I’m talking in general. Nobody can store CFrames in datastores, unless you “serialize” it.

EDIT: @MrCraeder2005 In case you don’t know how to “serialize” cframes, then I suggest you check out this post:

1 Like

I checked, you indeed can’t store CFrames.
However, you could probably chop up the values it contains into numbers and use that instead.

1 Like

Thank you, i’ll try this method.

1 Like

Also, to answer one of your questions:

Datastore can’t store CFrames. Its kinda like… storing lava in a box, the lava will quite literally burn the box up.

1 Like

What is the difference between Array and Table?

This is a table:

local tbl = {
something = "Idk"
}

This is an array:

local array = {
["Something"] = "Idk... Something. xD"
}

I am not too familiar with tables, and arrays, so the information that I’m giving you might not be correct.

2 Likes

Oh, thanks. I’ll change my script later.

1 Like

{CFrame:components()} is a way of storing cframe but it is HUGE
It is best to round off orientation and position and store those instead
As for other data types, only things like numbers, strings, booleans, nil, arrays, and dictionaries with string keys can be stored. Keep in mind that every character counts!!!
Storing 1000x copies of “0.2032039030240243” is many more characters than “0.2”

2 Likes

Just use tostring() before saving and then later, for getting it, do vector.new(position, position,position)

1 Like

what you can do is create a table of the data you want to be stored and use HttpService:JSONEncode to convert the data to a string and when u want to access the data use HttpService:JSONDecode to convert it back to readable table

1 Like

but data stores already do that, doing it before data stores do it will just increase the character count by 2