Saving multiple tables

Hello fellow developers,

I’ve wanted to save data for my upcoming building game, I used a simple code to do so:

Saving
for i, obj in ipairs(plot.itemHolder:GetDescendants()) do
			if not obj:IsA("Folder") or not obj:IsA("Configuration") then
				if obj.Name == "Wall" then
					table.insert(data, {
						[obj.Name] = {
							["name"] = obj.Name,
							["transform"] = {
								["x"] = obj.CFrame.X;
								["y"] = obj.CFrame.Y;
								["z"] = obj.CFrame.Z;
								["r"] = obj.Orientation.Y;
								["color"] = obj.Color
							}
						}

					})
				elseif obj.Name == "Floor" then
					table.insert(data, {
						[obj.Name] = {
							["name"] = obj.Name,
							["transform"] = {
								["x"] = obj.CFrame.X;
								["y"] = obj.CFrame.Y;
								["z"] = obj.CFrame.Z;
								["r"] = obj.Orientation.Y;
								["color"] = obj.Color
							}
						}

					})
				
				
				elseif string.find(obj.Name, "Roof") then
					if not obj:IsA("Folder") then
						table.insert(data, {
						[obj.Name] = {
								["name"] = obj.Name,
								["transform"] = {
									["x"] = obj.CFrame.X;
									["y"] = obj.CFrame.Y;
									["z"] = obj.CFrame.Z;
									["r"] = obj.Orientation.Y;
									["color"] = obj.Color
								}
							}

						})
					end
				
				
				elseif obj:IsA("Model") then
					if replicatedStorage.Objects:FindFirstChild(obj.Name, true) then
						local prim = obj.PrimaryPart
						
						table.insert(data, {
						[obj.Name] = {
								["name"] = obj.Name,
								["transform"] = {
									["x"] = prim.CFrame.X;
									["y"] = prim.CFrame.Y;
									["z"] = prim.CFrame.Z;
									["r"] = prim.Orientation.Y;
									["color"] = prim.Color
								}
							}

						})
					end
				end
			end
		end

This code gets all the descendants in the folder where all of the objects that were placed are parented to, then, it checks for the names, and sets each one in an individual table, as well as its own properties.

Then, when loading back:

Loading
if data then
		local plot = plr.Plot.Value
		for i, v in ipairs(data) do
			if v == "Wall" then
	            print("it's a wall")
		    dataloaded = true
		else
			dataloaded = true
			end
		end
	end

It loops through the loaded data, checking all the instances that have the name “Wall”. and printing if so, just for testing at the moment.

But, an error occurs when saving, it says that cannot store array in data store. data stores can only accept valid utf-8 characters

What can I do to make this code work? Any help is appreciated!

1 Like

You’re basicaly trying to save your data as the key you need to do it like this :

DataStore:SetAsync(key, data)

I believe that your key will be the userid of the player data !
I hope that was helpful have a nice day !

local success, err

		repeat
			success, err = pcall(function()
				dataStore:SetAsync(key, data)
			end)

			count = count + 1
		until count >= tries or success
1 Like

I don’t understand what you want to say by show me that ?

I wanted to say that I’m saving the data like you told me to.

1 Like

Use ProfileService module, it allows you to save arrays and such instead of just straight number and string keys, it is also more efficient than a regular datastore. I personally use it and it is very helpful in these kind of stuff.

What’s your key and what’s your data show me the entire script please !

I searched up what could be the answer and I think I found it !
You are trying to save a data structure but that’s impossible !
The one you are trying to save is a color3 and you basicaly can’t so just make another dictionnary with the r, g, and b !
I hope that was helpful have a nice day !