Saving EVERY SINGLE World Data

I’ve been working on a Blockate recreation (https://www.roblox.com/games/1174068786/Blockate), and I’ve got the basic system done, But I need to know how to save world data. How would I save EVERY SINGLE world’s data, and get data for worlds that have a specific data table item?

To simplify that, how would I save every single world’s data, while still being able to get specific world data?

If you are attempting to help, please include explanations and/or code examples. I may be a bit dumb, or annoying, but please try and help. Any help is appreciated.

1 Like

you could do a simple script such as this

local datastore = game:GetService("DataStoreService"):GetDataStore("YourDataStore")

-- save the properties of the part
local function serializePart(part)
    return {
        className = part.ClassName,
        position = part.Position,
        size = part.Size,
        orientation = part.Orientation,
        color = part.Color,
        material = part.Material.Name,
        anchored = part.Anchored,
        canCollide = part.CanCollide,
        name = part.Name,
    }
end

-- creates a new instance based on properties of the part
local function deserializePart(data)
    local part = Instance.new(data.className)
    part.Position = data.position
    part.Size = data.size
    part.Orientation = data.orientation
    part.Color = data.color
    part.Material = Enum.Material[data.material]
    part.Anchored = data.anchored
    part.CanCollide = data.canCollide
    part.Name = data.name
    return part
end

-- save all BaseParts in the workspace
local saveTable = {}
for _, v in ipairs(workspace:GetDescendants()) do
    if v:IsA("BasePart") then
        table.insert(saveTable, serializePart(v))
    end
end

datastore:SetAsync("key", saveTable)

-- load all BaseParts from the datastore into the workspace
local data = datastore:GetAsync("key")
if data then
    for _, partData in ipairs(data) do
        local part = deserializePart(partData)
        part.Parent = workspace
    end
end

this would save every part in the workspace (or a selected folder) and then you can load them whenever you want

2 Likes

Depends on the data. You should try to identify the patterns within said data, how many different states and configurations are there? This highly impacts the size of the data you actually have to save in order to load in a perfect copy.

Assuming the player somehow can edit all the properties of a part(which I highly doubt, most often they’re just a few), you can try serializing the world. Serializing is a way of converting instances/objects to strings, so they can be saved in a datastore. Afterwards if you notice that the total size of the string is large you can try compressing it to reduce its size(serialization data can often compress really well due to the large amount of patterns within it). Currently datastores allow 4MB of data per key, this means that your data after HttpService:JSONEncode(data) runs should have a maximum of 4 million characters/string length.

1 Like

This is a good example, but how would I get a certain world’s key by checking if the world data has a specific boolean?

Extra note, what I was thinking for saving data, is that I can save the main data inside the player. Like:

local TotalWorlds = {
    World1,
    World2,
    World3,
    -- etc.
}
local WorldData = {
    [World1] = {...},
    [World2] = {...},
    [World3] = {...},
    -- etc.
}

Then once the player joins a world, it’ll check which world was joined. For example, World3 was the one that was joined. It’ll see if it exists in the ‘TotalWorlds’ table. IF it does, it’ll see if it has data for it inside the ‘WorldData’ table. Then it will load all the data. But I don’t know if it’s efficient if I’m being honest.

I just don’t know how I’m gonna save it either.