Problem with copying terrain

Hello
I have a part and terrain around it.
I want to copy the terrain around the part, delete it and later I want to paste it at the exact same place where it was.
This is the code ,which I am using

local radius = 300
local pos = workspace.Part.Position
local region = Region3int16.new(Vector3int16.new(pos.X-radius, pos.Y-radius, pos.Z-radius), Vector3int16.new(pos.X+radius, pos.Y+radius, pos.Z+radius))
local terrainData = workspace.Terrain:CopyRegion(region)

workspace.Terrain:Clear()
task.wait(5)
workspace.Terrain:PasteRegion(terrainData, region.Min, false)

This works perfectly fine when the part and the terrain are at the center of the map, but does not work at all when the part and the terrain are 2000 studs away from the center.
The terrain does not appear at all during the Paste.
What do I miss?

Thank you in advance

3 Likes

could be to do with the property streaming enabled in workspace being enabled. Try disabling that

Streaming is disabled since the beginning

1 Like

You got the illusion your code was working, since the workspace.Part.Position is not related to the terrain coordinates. With a radius big enough you will copy the entire terrain and paste it back after clearing. You need to convert Part.Position to Terrain coordinates as this example:

	local radius = 30
	local pos = terrain:WorldToCell(workspace.Part.Position)
	local region = Region3int16.new(Vector3int16.new(pos.X-radius, pos.Y-radius, pos.Z-radius), Vector3int16.new(pos.X+radius, pos.Y+radius, pos.Z+radius))
	local terrainData = workspace.Terrain:CopyRegion(region)

	workspace.Terrain:Clear()
	task.wait(5)
	workspace.Terrain:PasteRegion(terrainData, region.Min, false)
1 Like

But what about the region?
does it also contain voxel coordinates?
if yes, how can I calculate the radius (I have its dimension in studs)?

I already fixed it for you. It`s simple just use the WorldToCell to convert the Part position to terrain coordinates.

1 Like

Thanks a lot
but still I wonder whether I am copying bigger than necessary radius by defining the radius in studs?

2 Likes

It is fine. The problem was the part. Position. Since that coordinates could not be used to calculate the region. If you replace your code with mine, will work just fine. Don’t matter where you put the Part.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.