Saving player homes

I am looking into trying to save things that players build in my games. For example, a system in which players could decorate their homes with furniture. I have tried doing this in the past and I have just kept running into problems, and I am not sure what the best way to be doing this really is.

It should be worth noting that I don’t expect player houses to be exactly where they were on the map in the previous server (i.e. like Bloxburg, where the house can move onto a different plot).

Thus far, I have been trying to just create a module responsible for compiling all of it in a table, and using HttpService to encode the table in JSON. Here’s an example:

function Encode(Directory)
    local SaveTable = {}
    for _, Object in pairs(Directory:GetDescendants()) do
        if Object:IsA("Part") then
            local ObjectData = {}
            ObjectData.Position = Object.Position
            ObjectData.Size = Object.Size
            -- and so on
            table.insert(SaveTable, ObjectData)
        end
    end
    
    local JSONEncode = game:GetService("HttpService"):JSONEncode(SaveTable)
    return JSONENcode
end

I don’t know if there is a better way to be doing this, or if there is a preexisting module that I should be looking into instead.

1 Like

Trying to save Parts like that only really makes sense if you allow players to build any kind of thing with building tools. If you just allow players to place pre-made assets on a grid, then you can get away with only storing

  • Placement coordinates as two ints (X and Z)
  • Rotation in number of increments as one int (e.g. 90 degree increments = the numbers 0, 1, 2, 3)
  • Asset ID as one int

Storing homes like this would let you get away with as little as like 8 bytes per placed asset, depending on the complexity of what you’re trying to do. You can use BitBuffer to save ints and other data types with any number of bits of precision you want.

If you were to store the Positions and Sizes like they are, it could be anywhere from a few characters per coordinate (e.g. “(22, -5, 10”)) to loooooots of chars per coordinate (e.g. “(22.00000001,-5.100213012, 10.1232131)”). Plus each asset is probably made of MANY parts, so doing a less flexible but more customized saving schema will probably let you store 100-1000 times as much stuff.

BUT if you test it and saving Parts as position and size in JSON is not a problem, then don’t spend time optimizing.

Would it be worth setting up object oriented programming modules for every premade object, and just create my own custom properties so all I am saying is the class and position?

That wouldn’t give you any kind of advantage. The point is just that you’re saving only the information you need to be able to reconstruct the saved home when it’s supposed to be loaded. Using an OOP approach doesn’t help with that in any way.

If there’s a limited number of pre-made furniture, then you can assign each one a number. I would make each placeable asset a Model and put an IntValue object inside, called “AssetId”. I’d then put all of them in a Folder in ReplicatedStorage or ServerStorage, and clone those when furniture is placed. Then you just save that AssetId’s Value instead of all of the parts, along with position and rotation of the placed furniture.