Copying and pasting terrain from region3int16

Hello, I have an issue where I can’t copy a piece of terrain that is in a region3int16 which has been calculated from a part position and size, the script is basically like this:

-- RegionPart is just a base part which is located inside terrain
local CornerA = RegionPart.Position - (RegionPart.Size / 2)
local CornerB = RegionPart.Position + (RegionPart.Size / 2)
		
local _Region3int16 = Region3int16.new(
	Vector3int16.new(CornerA.X, CornerA.Y, CornerA.Z),
	Vector3int16.new(CornerB.X, CornerB.Y, CornerB.Z)
)

local _TerrainRegion = Terrain:CopyRegion(_Region3int16)
do
	_TerrainRegion.Name = RegionPart.Name
	-- RegionsFolder is a folder in which pieces of terrain are stored in
	_TerrainRegion.Parent = RegionsFolder
end

Terrain:PasteRegion(_TerrainRegion, Vector3int16.new(0, 0, 0), false)

The terrain just doesn’t appear anywhere, I tried using the script from the docs that worked but only for the whole terrain

while task.wait(2) do
	local terrainRegion = workspace.Terrain:CopyRegion(workspace.Terrain.MaxExtents)
	workspace.Terrain:Clear()
	task.wait(1)
	local corner = Vector3int16.new(-32500, -32000, -32000)
	local pasteEmptyRegion = false
	workspace.Terrain:PasteRegion(terrainRegion, corner, pasteEmptyRegion)
end

I tried to read and write the verticies of the terrain but it was much slower (and also didn’t work for a specific region) because I need to store this information somewhere

Now, regarding your issue with the Lua code, it seems that the problem might be in the way the region is defined. One possible solution is to use math.floor to get the correct integer values for the Vector3int16.new function. Here’s an updated version of your code:

-- RegionPart is just a base part which is located inside terrain
local CornerA = RegionPart.Position - (RegionPart.Size / 2)
local CornerB = RegionPart.Position + (RegionPart.Size / 2)

local _Region3int16 = Region3int16.new(
    Vector3int16.new(math.floor(CornerA.X), math.floor(CornerA.Y), math.floor(CornerA.Z)),
    Vector3int16.new(math.floor(CornerB.X), math.floor(CornerB.Y), math.floor(CornerB.Z))
)

local _TerrainRegion = Terrain:CopyRegion(_Region3int16)
do
    _TerrainRegion.Name = RegionPart.Name
    -- RegionsFolder is a folder in which pieces of terrain are stored in
    _TerrainRegion.Parent = RegionsFolder
end

Terrain:PasteRegion(_TerrainRegion, Vector3int16.new(0, 0, 0), false)

With this modification, the region should be calculated correctly, and you should be able to copy the piece of terrain without any issues. Let me know if you need any further assistance with this.

It still doesn’t work, when I tried to load the terrain region object using a plugin I found on the marketplace It just loaded “air” into some seemingly random position

It seems like the issue may be related to the fact that the _TerrainRegion object is not being placed in the correct position when it is pasted back into the terrain.

Assuming that you want to paste the region back into the terrain at the same position as the original RegionPart, you can modify your code as follows:

local CornerA = RegionPart.Position - (RegionPart.Size / 2)
local CornerB = RegionPart.Position + (RegionPart.Size / 2)

local _Region3int16 = Region3int16.new(
    Vector3int16.new(math.floor(CornerA.X), math.floor(CornerA.Y), math.floor(CornerA.Z)),
    Vector3int16.new(math.floor(CornerB.X), math.floor(CornerB.Y), math.floor(CornerB.Z))
)

local _TerrainRegion = Terrain:CopyRegion(_Region3int16)
_TerrainRegion.Name = RegionPart.Name

local offset = _TerrainRegion.CFrame:ToObjectSpace(RegionPart.CFrame)
local pastePosition = Vector3int16.new(math.floor(offset.X), math.floor(offset.Y), math.floor(offset.Z))

Terrain:PasteRegion(_TerrainRegion, pastePosition, false)

In this modified code, we calculate the offset between the center of the _TerrainRegion object and the center of the original RegionPart using the CFrame:ToObjectSpace() method. We then use this offset to calculate the pastePosition, which represents the position where the _TerrainRegion object should be pasted back into the terrain.

Note that this assumes that the RegionPart and _TerrainRegion objects have the same orientation. If they don’t, you’ll need to adjust the offset calculation accordingly.

It says that CFrame is not a valid member of TerrainRegion

Here is the updated code:

local CornerA = RegionPart.Position - (RegionPart.Size / 2)
local CornerB = RegionPart.Position + (RegionPart.Size / 2)

local _Region3 = Region3.new(CornerA, CornerB)

local _TerrainRegion = Terrain:CopyRegion(_Region3)
_TerrainRegion.Name = RegionPart.Name

local center = _Region3.CFrame.Position
local offset = _TerrainRegion.CFrame:ToObjectSpace(CFrame.new(center))
local pastePosition = Vector3int16.new(math.floor(offset.X), math.floor(offset.Y), math.floor(offset.Z))

Terrain:PasteRegion(_TerrainRegion, pastePosition, false)

1 Like

sadly it still doesn’t work because the Region3int16 and TerrainRegion don’t have a cframe property

You are correct that Region3int16 and TerrainRegion do not have a CFrame property. Instead, you can use the Position property of the Region3int16 to determine the center of the region in the world space, and then calculate the offset between the center of the copied region and the position where you want to paste it.

Here’s an updated version of the code that should work:

local CornerA = RegionPart.Position - (RegionPart.Size / 2)
local CornerB = RegionPart.Position + (RegionPart.Size / 2)

local _Region3 = Region3.new(CornerA, CornerB)

local _TerrainRegion = Terrain:CopyRegion(_Region3)
_TerrainRegion.Name = RegionPart.Name

local center = _Region3.CFrame.Position
local offset = _TerrainRegion.CFrame:ToObjectSpace(CFrame.new(center))
local pastePosition = Vector3int16.new(math.floor(offset.X), math.floor(offset.Y), math.floor(offset.Z))

Terrain:PasteRegion(_TerrainRegion, pastePosition, false)

This should paste the copied region at the center of the RegionPart, without any approximation errors.

Bro, are you using chat gpt or something to generate those answers? I checked and you got like 90% of probability

Nope, I’am just using some grass and benzos to boost this cool afternoon.

So you are just high? Ok nvm xD. BTW I just checked your profile, you kinda don’t hide this fact do you?

1 Like

I’ve had to do this before to store terrain maps, but I don’t see any issue with your code. It’s probably because the extents are wrong.

This is supposed to be the corner. Unfortunately, this needs to be aligned with your extents. It will not transform the terrain into a different position.

Could you elaborate please? Is it like a different origin of the terrain or something?

No, it’s the corner of the terrain, not the origin.