Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how can i load in my terrain using a script?

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

1 answer

✓ Accepted answer

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:

ModuleScript

15 comments

1

and you said you are bad at scripting... ;-;

0

hey i put the code you gave me in a script located under ServerScriptService and "includeWaterProperties" is coming up as unknown, how do i set it to true or what do i need to do for it to be defined, thanks.

0

It pretty easy. You can do like this: terrainSaveLoad:Save(true) to set it to true and vice versa.

0

And @Ngyuenlegiahung, yes I'm bad at scripting :)

0

i may sound very dumb but i don't know what you mean. please take more time to help me i would be very thankful, i have this in a script:                                                    local terrainSaveLoad = require(game.ServerScriptService.TerrainSaveLoad) terrainSaveLoad:Save(true)     local savedTerrain = terrainSaveLoad:Save(includeWaterProperties)                 terrainSaveLoad:Load(sav

0

I think I should re-write the answer...

0

yes please

0

i pasted that code into a script and the "includeWaterProperties" is "unknown", do i need to set it as a variable or something so that it reconizes it?

0

You don't need to do that. You can do local savedTerrain = terrainSaveLoad:Save(true) directly.

0

i get this error: 01:24:55.430 - The current identity (2) cannot Set (lacking permission 1)

0

I never seen this error before. Check is there any terrain in your game right now.

0

i want some terrain i have to be copy and pasted through a script but i don't understand how i can do that with the script that is given with the plugin. could you please create some code that i could put into a script that would do that.

0

yes their is terrain in my map right now, this is the code i have in the other script and i have not modified any code in the module script : local terrainSaveLoad = require(game.ServerScriptService.TerrainSaveLoad) -- require the module so we can use it local savedTerrain = terrainSaveLoad:Save(true) -- this will save the current terrain terrainSaveLoad:Load(savedTerrain) -- This will load the

0

Maybe the plugin out-dated...

0

Possibly : / . i just really want to put terrain in my game which i can clone / load. thanks for your time though

This discussion is preserved as it was. New answers, comments, and votes are closed.