Hello, i have made a terrain for my game and i'm currently using a plugin to save / load the terrain in manually but i don't know how to use the script to load in the terrain automatically / through a script. Their is some what of a set of instructions but its very broad and minimal and i don't understand it, help would be very much appreciated :)
-- Terrain Save & Load -- Crazyman32 -- January 17, 2015
--[[
How to use: - Keep this under ServerScriptService or ServerStorage - Use from a Script on the server (not LocalScript) Example: local terrainSaveLoad = require(game.ServerScriptService.TerrainSaveLoad) -- Save terrain: local savedTerrain = terrainSaveLoad:Save(includeWaterProperties) -- Saves water properties too if "includeWaterProperties" is 'true' -- Load terrain: terrainSaveLoad:Load(savedTerrain)
--]]
local TerrainSaveLoad = { Version = "1.0.3"; }
function TerrainSaveLoad:Save(includeWaterProperties)
local t = game.Workspace.Terrain -- Copy terrain: local tr = t:CopyRegion(t.MaxExtents) tr.Name = "SavedTerrain" tr.Parent = game.Workspace -- Save water properties: if (includeWaterProperties) then local waterProps = Instance.new("Configuration", tr) waterProps.Name = "WaterProperties" local function SaveProperty(class, name) local p = Instance.new(class, waterProps) p.Name = name xpcall(function() p.Value = t[name] end, function(err) print("Failed to get property: " .. tostring(err)) end) end SaveProperty("Color3Value", "WaterColor") SaveProperty("NumberValue", "WaterReflectance") SaveProperty("NumberValue", "WaterTransparency") SaveProperty("NumberValue", "WaterWaveSize") SaveProperty("NumberValue", "WaterWaveSpeed") end -- Set as selected: game:GetService("Selection"):Set({tr}) -- Return the TerrainRegion copy: return tr
end
function TerrainSaveLoad:Load(terrainRegion)
-- Ensure 'terrainRegion' is correct: assert(typeof(terrainRegion) == "Instance" and terrainRegion:IsA("TerrainRegion"), "Load method for TerrainSaveLoad API requires a TerrainRegion object as an argument" ) -- Find center position: local xPos = -math.floor(terrainRegion.SizeInCells.X * 0.5) local yPos = -math.floor(terrainRegion.SizeInCells.Y * 0.5) local zPos = -math.floor(terrainRegion.SizeInCells.Z * 0.5) local pos = Vector3int16.new(xPos, yPos, zPos) -- Load water properties: local waterProps = terrainRegion:FindFirstChild("WaterProperties") if (waterProps) then local function LoadProperty(name) local obj = waterProps:FindFirstChild(name) if (not obj) then return end xpcall(function() game.Workspace.Terrain[obj.Name] = obj.Value end, function(err) print("Failed to set property: " .. tostring(err)) end) end LoadProperty("WaterColor") LoadProperty("WaterReflectance") LoadProperty("WaterTransparency") LoadProperty("WaterWaveSize") LoadProperty("WaterWaveSpeed") end -- Load in the terrain: game.Workspace.Terrain:PasteRegion(terrainRegion, pos, true)
end
return TerrainSaveLoad
The script you provide above is a ModuleScript. If you don't know, ModuleScript is a type of script. ModuleScript can't run by itself, but is can be require by another script to use the function, data,... of the Module. For example:
ModuleScript(located in ReplicateStorage, named "ModuleScript"):
local MyModule = {} --we use table to store many function, value,... of the module function MyModule.PrintStuff() print("stuff") end -- this will add the function to the table return MyModule -- we need to return the table so we can use module
Script(located in WorkSpace):
local MyModule = require(game.ReplicateStorage.ModuleScript) -- require will get the module, so we can use it MyModule.PrintStuff() -- this will run the function of the module
Output:
stuff
Now, you know the basic of ModuleScript, you can use the "Save and Load Terrain" module. Here is the example of how to use it: Script:
local terrainSaveLoad = require(game.ServerScriptService.TerrainSaveLoad) -- require the module so we can use it local savedTerrain = terrainSaveLoad:Save(includeWaterProperties) -- this will save the current terrain -- terrainSaveLoad:Save(true) to set includeWaterProperties to true and vice versa terrainSaveLoad:Load(savedTerrain) -- This will load the savedTerrain
Hope this helped you :D
Some document: