Can't store array of serialized objects in datastore

Hello everyone. I’m making a level creator game, and it works in this way: In ServerStorage, there’s a folder of all the elements a player could add to their level. I’m trying to keep it simple for the moment, so there’s only one of them (called “Block”), as seen down here

Immagine 2023-05-18 170019

During the level creation, the player will create duplicates of that block and place them around.

in order for it to save all of the elements a player placed (in this case, only blocks) when the player decides to save, a function turns each element in an array containing its properties (it’s something i’ve read about in an article here on the devHub, called serializing), since you can’t save instances to a DataStore.

local function serialize(object: Instance)

	local objectData = {
		ElementId = object:GetAttribute("ElementId"),
		Position = object.Position
	}

	return objectData

end

local function deserialize(objectData)

	local object

	if elemRefs:FindFirstChild(objectData.ElementId) then

		object = elemRefs:FindFirstChild(objectData.ElementId):Clone()

		object:SetAttribute("ElementId", objectData.ElementId)
		object.Position = objectData.Position
		object.Parent = workspace.Elements

	end

end

as a note, ElementId is an identifier used to fetch the corresponding object from ReplicatedStorage, and Elements is a folder in Workspace that is used to store all of the custom elements a player uses.

Now, this is where I’m having trouble…

Basically, when I want the script to save all the elements, I create an array of all the serialized elements and attempt to save it. This is the function that makes this array:

local function saveLevelAsyncs()
	
	local newElems = {}
	
	for i, v in ipairs(workspace.Elements:GetChildren()) do
		table.insert(newElems, serialize(v))
	end
	
	local success, eMessage = pcall(function()
		myDS:SetAsync("LevelElements-"..tostring(levelCode), newElems)
	end)
	
	if success then
		print("[MainScript]: Level elements saved successfully")
		return true
	else
		warn("[MainScript]: Couldn't save level elements: "..eMessage)
		return false
	end
	
end

I can never make it work, because the pcall always throws this error: 104: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters.

I suspect this has something to do with making an array of arrays, does anyone know about possible solutions or workarounds? It would be greatly appreciated, thanks in anticipation

datatstores can only store primitive data types: strings, booleans, ints and doubles

1 Like

Position returns a Vector3 which is not a serialised value. You’re going to need to save its XYZ components.

3 Likes

Maybe you using iPairs loop that changes something in array, try to use in pairs loop, this is zero difference if you using universal scripting, but if you using direct scripting, try to look on youtube or dev forum

thank you so much! It works now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.