Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how can i load in my terrain using a script?

Asked by 5 years ago

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

--[[

01How to use:
02 
03- Keep this under ServerScriptService or ServerStorage
04- Use from a Script on the server (not LocalScript)
05 
06 
07Example:
08 
09    local terrainSaveLoad = require(game.ServerScriptService.TerrainSaveLoad)
10 
11    -- Save terrain:
12    local savedTerrain = terrainSaveLoad:Save(includeWaterProperties)
13 
14        -- Saves water properties too if "includeWaterProperties" is 'true'
15 
16 
17    -- Load terrain:
18    terrainSaveLoad:Load(savedTerrain)

--]]

local TerrainSaveLoad = { Version = "1.0.3"; }

function TerrainSaveLoad:Save(includeWaterProperties)

01local t = game.Workspace.Terrain
02 
03-- Copy terrain:
04local tr = t:CopyRegion(t.MaxExtents)
05    tr.Name = "SavedTerrain"
06    tr.Parent = game.Workspace
07 
08-- Save water properties:
09if (includeWaterProperties) then
10    local waterProps = Instance.new("Configuration", tr)
11    waterProps.Name = "WaterProperties"
12    local function SaveProperty(class, name)
13        local p = Instance.new(class, waterProps)
14        p.Name = name
15        xpcall(function()
View all 32 lines...

end

function TerrainSaveLoad:Load(terrainRegion)

01-- Ensure 'terrainRegion' is correct:
02assert(typeof(terrainRegion) == "Instance" and terrainRegion:IsA("TerrainRegion"),
03    "Load method for TerrainSaveLoad API requires a TerrainRegion object as an argument"
04)
05 
06-- Find center position:
07local xPos = -math.floor(terrainRegion.SizeInCells.X * 0.5)
08local yPos = -math.floor(terrainRegion.SizeInCells.Y * 0.5)
09local zPos = -math.floor(terrainRegion.SizeInCells.Z * 0.5)
10local pos = Vector3int16.new(xPos, yPos, zPos)
11 
12-- Load water properties:
13local waterProps = terrainRegion:FindFirstChild("WaterProperties")
14if (waterProps) then
15    local function LoadProperty(name)
View all 32 lines...

end


return TerrainSaveLoad

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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"):

01local MyModule = {}
02--we use table to store many function, value,... of the module
03 
04function MyModule.PrintStuff()
05    print("stuff")
06end
07-- this will add the function to the table
08 
09return MyModule
10-- we need to return the table so we can use module

Script(located in WorkSpace):

1local MyModule = require(game.ReplicateStorage.ModuleScript)
2-- require will get the module, so we can use it
3 
4MyModule.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:

1local terrainSaveLoad = require(game.ServerScriptService.TerrainSaveLoad)
2-- require the module so we can use it
3 
4local savedTerrain = terrainSaveLoad:Save(includeWaterProperties)
5-- this will save the current terrain
6-- terrainSaveLoad:Save(true) to set includeWaterProperties to true and vice versa
7 
8terrainSaveLoad:Load(savedTerrain)
9-- This will load the savedTerrain

Hope this helped you :D

Some document:

ModuleScript

1
and you said you are bad at scripting... ;-; Nguyenlegiahung 1091 — 5y
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. Code1400 75 — 5y
0
It pretty easy. You can do like this: terrainSaveLoad:Save(true) to set it to true and vice versa. Block_manvn 395 — 5y
0
And @Ngyuenlegiahung, yes I'm bad at scripting :) Block_manvn 395 — 5y
View all comments (11 more)
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 Code1400 75 — 5y
0
I think I should re-write the answer... Block_manvn 395 — 5y
0
yes please Code1400 75 — 5y
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? Code1400 75 — 5y
0
You don't need to do that. You can do local savedTerrain = terrainSaveLoad:Save(true) directly. Block_manvn 395 — 5y
0
i get this error: 01:24:55.430 - The current identity (2) cannot Set (lacking permission 1) Code1400 75 — 5y
0
I never seen this error before. Check is there any terrain in your game right now. Block_manvn 395 — 5y
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. Code1400 75 — 5y
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 Code1400 75 — 5y
0
Maybe the plugin out-dated... Block_manvn 395 — 5y
0
Possibly : / . i just really want to put terrain in my game which i can clone / load. thanks for your time though Code1400 75 — 5y
Ad

Answer this question