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!