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?
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.
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.
{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”
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