#Terrain region copy pasting?

1 messages · Page 1 of 1 (latest)

long grove
#

i've looked at the dev docs for terrainrgion but i dont think im doing it correctly, and i cant find anywhere that specifically goes into detail on it for scripting.

Im trying to clone the terrain and place it into workspace alongside buildings & etc for map changing but it doesnt seem to be working. TIA

local RS = game:GetService("ReplicatedStorage")
local LoadedMapFolder = workspace.LoadedMaps

local MapsFolder = RS.Maps
local LoadMapEvent = RS.remotes.LoadMap
local Terrain = workspace.Terrain

local function clearPreviousMap()
    for _, item in ipairs(LoadedMapFolder:GetChildren()) do
        item:Destroy()
    end
end

LoadMapEvent.OnServerEvent:Connect(function(player, mapName)
    print("Server received map name:", mapName)

    local mapTemplate = MapsFolder:FindFirstChild(mapName)
    if not mapTemplate then
        warn("no map named", mapName)
        return
    end

    clearPreviousMap()

    local mapClone = mapTemplate:Clone()
    local terrainRegion = mapTemplate:FindFirstChild("SavedTerrain")

    if terrainRegion then
        local region = Region3.new(terrainRegion.Position, terrainRegion.Position + terrainRegion.Size)
        local regionCopy = Terrain:CopyRegion(region)
        mapClone.Parent = LoadedMapFolder
        Terrain:PasteRegion(regionCopy)
        print("map load with terrain region:", mapName)
    else
        mapClone.Parent = LoadedMapFolder
        print("map load without terrain:", mapName)
    end
end)


teal drum
# long grove i've looked at the dev docs for terrainrgion but i dont think im doing it correc...

What exactly is happening? Any errors?

Heres one possible issue, Region3's take a min point and a max point (aka two opposite corners) and the way your converting part -> region is treating the center of the part as the corner of the region

This would be the correct way

local function PartToRegion(part: BaePart): Region3
  local radius = part.Size / 2
  local pos = part.Position
  return Region3.new(pos - radius, pos + radius)
end

local region = PartToRegion(terrainRegion)
long grove
#

With your snippet provided the only error that occurs is:
18:17:04.884 Size is not a valid member of TerrainRegion ReplicatedStorage.Maps.jeezaline.SavedTerrain

Heres my workspace if that matters at all & an updated script with your snippet added.|

local RS = game:GetService("ReplicatedStorage")
local LoadedMapFolder = workspace.LoadedMaps

local MapsFolder = RS.Maps
local LoadMapEvent = RS.remotes.LoadMap
local Terrain = workspace.Terrain

local function clearPreviousMap()
    for _, item in ipairs(LoadedMapFolder:GetChildren()) do
        item:Destroy()
    end
end

local function PartToRegion(part: BasePart): Region3
    local radius = part.Size / 2
    local pos = part.Position
    return Region3.new(pos - radius, pos + radius)
end

LoadMapEvent.OnServerEvent:Connect(function(player, mapName)
    print("Server received map name:", mapName)

    local mapTemplate = MapsFolder:FindFirstChild(mapName)
    if not mapTemplate then
        warn("no map named", mapName)
        return
    end

    clearPreviousMap()

    local mapClone = mapTemplate:Clone()
    local terrainRegion = mapTemplate:FindFirstChild("SavedTerrain")

    if terrainRegion then
        local region = PartToRegion(terrainRegion)
        local regionCopy = Terrain:CopyRegion(region)
        mapClone.Parent = LoadedMapFolder
        Terrain:PasteRegion(regionCopy)
        print("map load with terrain region:", mapName)
    else
        mapClone.Parent = LoadedMapFolder
        print("map load without terrain:", mapName)
    end
end)

#

it prints up to "server recieved map name" but after that comes the error.

teal drum
#

leme read the docs rq I think I'm missing something

long grove
teal drum
#

It might be unwise to be cloning TerrainRegion instances because they are probably pretty memory intensive

    local mapClone = mapTemplate:Clone()
    local terrainRegion = mapTemplate:FindFirstChild("SavedTerrain")
#

Try replacing this code

        local region = PartToRegion(terrainRegion)
        local regionCopy = Terrain:CopyRegion(region)
        mapClone.Parent = LoadedMapFolder
        Terrain:PasteRegion(regionCopy)

with

        Terrain:PasteRegion(terrainRegion, Vector3int16.new(0, 0, 0), true)
        mapClone.Parent = LoadedMapFolder
long grove
#

it still seems to be only loading the map and not terrain, But it does say "map load with terrain region: jeezaline" (maps name), after testing it a bit it does it for all the maps but as i said still no terrain

#

no errors or anything from that script either, just the prints saying it recived the fireserver, and that the map was loaded with terrain region.

teal drum
long grove
#

21:47:24.185 64001, 64001, 64001 - Server - MapLoader:30

#

Sorry for the late response i got caught up with school

teal drum
#

I wonder if the entire region is just air for some reason

#

How did you create the terrainRegions?

teal drum
long grove
long grove