Script execution timeout while copying small terrain region

Problem

Copying small terrain region (30x12x30) causes script timeout.

System Information

CPU: 12th Gen Intel(R) Core™ i9-12900K
GPU: NVIDIA GeForce RTX 4080
Memory: 32 GB

Visual Aids

Reproduction Files

TerrainPractice.rbxl (54.2 KB)

2 Likes

Fixed it:

local offset = Vector3.one/2
local function getCorners(part: BasePart)
	return {
		trForward = part.Position + part.Size*offset  ,
		blBack = part.Position + part.Size*(-offset)  
	}
end

Actually i should try pasting it to ensure its right

Yeah no I can’t get it to paste so maybe I ended up copying nothing and thats why the error is gone. Or I am pasting wrong.

2 Likes

EUREKA!!!

--!strict
local copyPart = game.Workspace.CopyPart
local destinationPart = game.Workspace.DestinationPart

local offset = Vector3.one/2
local function getCorners(part: BasePart)
	return {
		trForward = part.Position + part.Size*offset  ,
		blBack = part.Position + part.Size*(-offset)  
	}
end

local function convertVector3ToVector3int16(vector: Vector3)
	return Vector3int16.new(vector.X, vector.Y, vector.Z)
end

local function convertVintToV(vector: Vector3int16)
	return Vector3.new(vector.X, vector.Y, vector.Z)
end

--Divide by 4 to align with voxel coordinate system
local function getRegion3Int16FromVector3(min: Vector3, max: Vector3)
	return Region3int16.new(convertVector3ToVector3int16(min)/4, convertVector3ToVector3int16(max)/4)
end

local copyPartCorners = getCorners(copyPart)
local copyRegion = getRegion3Int16FromVector3(copyPartCorners.blBack, copyPartCorners.trForward)

print('Copied region:', copyRegion)

local copiedTerrainRegion = game.Workspace.Terrain:CopyRegion(copyRegion)
workspace.Terrain:Clear()

local destinationCorners = getCorners(destinationPart)
local destinationRegion = getRegion3Int16FromVector3(destinationCorners.blBack, destinationCorners.trForward)

print('Destination region:', destinationRegion)

game.Workspace.Terrain:PasteRegion(copiedTerrainRegion, destinationRegion.Min, true)

2 Likes

This works thank you!

I believe the docs need to be updated to reflect this information. Its not immediately clear that we need to divide the region we’re attempting to copy by 4. On top of that the script timeout itself makes me think that its a bug. If a staff member can confirm that would be great!

1 Like

I’d imagine it times out because its trying to copy an infinite number of voxels.

Each time it checks if its reached the max, but if the max is defined wrong, it can never finish it. Your limits were defined wrong so it could never reach the limit thus imploded.

Roblox should easily be able to add a check to return a more suitable error if the limit is unreachable.

In trying to solve this, i realised how many people have, for years, tried to wrap their head around this confusing part of the Terrain API. Yeah a documentation update would be useful. Ive not read the Region3 documentation, perhaps it mentions that it uses the voxel coordinates instead. Still, a reminder to consider this would be useful in the PasteRegion documentation etc.

2 Likes

This is just an acknowledgment announcement!

We’ve filed a ticket to our internal database, and we’ll follow up when we have an update!

Thank you for the report!

1 Like