Saving Terrain in Data Store

How can I save Terrain in a datastore? Heres an example of what I would want to save.

I know I’m going to use this code, but how would I save the terrain data in data store?

local terrainRegion = workspace.Terrain:CopyRegion(workspace.Terrain.MaxExtents)
3 Likes

Edit:

You can’t save/load terrain data directly in DataStores.

You can, however, read the terrain data via ReadVoxels and then save the returned data (the material and occupancy data tables) in a DataStore. Then, when you want to, you can load the data and use WriteVoxels to actually load the terrain into the game.

It should be noted that you can’t save Enum values (the materials table holds enum values) in DataStores directly, so you’ll need to convert the material enums to their respective string/number values and save that instead. Then, when you’re loading the saved data, you’ll need to convert those string/numbers to their respective material enums so that WriteVoxels will read the materials table correctly.

10 Likes

Not sure I understand the logic of saving terrain in a Datastore. I use CrazyMan32’s “Save $ Load Terrain” plugin to export my terrain, than copy the resultant save to ServerStorage. Each map change, then deletes the current terrain and loads the new in.

This is achieved using:

workspace.Terrain:Clear()
workspace.Terrain:PasteRegion(terrainSource, workspace.Terrain.MaxExtents.Min, true)

4 Likes

How did you do that? because I want to make a game where the map changes and loads new terrain in.

1 Like

Argh. Zombie thread rises from up…

  1. In Studio, go to Toolbox and search for “Terrain Load and Save”. The correct one is made by sleitnick (was CrazyMan32). Install the plugin
  2. Generate your Terrain in Studio
  3. Once happy, open the “Terrain Load and Save” (Menu bar > Plugins) and click Save
  4. In your Workspace, you will now see a “SavedTerrain” item, move this to ServerStorage. You now have a saved Terrain that you can load in whenever.
  5. To load a new Terrain use:
workspace.Terrain:Clear()    -- Removes current Terrain
workspace.Terrain:PasteRegion(<Terrain Source & Name>, workspace.Terrain.MaxExtents.Min, true)

For example

local SS = game:GetService("ServerStorage")
local terrains = {SS.Terrain1, SS.Terrain2, SS.Terrain3}    -- A list of Terrain sources in ServerStorage
local newTerrain = math.random(1, #terrains)   -- Choose a new terrain at random from the table
workspace.Terrain:Clear()
workspace.Terrain:PasteRegion(newTerrain, workspace.Terrain.MaxExtents.Min, true)    -- Loads a new Terrain
12 Likes

Sorry about this, as it’s been awhile but when I do this from a script in serverscript, it says “Unable to cast object”, and I don’t know why.
image

Share your script on here so we can see what might be causing the problem.

It’s the same script you provided, nothing changed at all about it.

Have you exported the terrain you have created using CrazyMan32’s “Save $ Load Terrain” plugin?
If you have, then you just need to make sure that the list of terrains is pointing to the right locations in this line:

local SS = game:GetService("ServerStorage")
local terrains = {SS.Terrain1, SS.Terrain2, SS.Terrain3}
1 Like