Error when I try adding to an array

When my script adds to the terrainCoordinates array it errors, which always occurs at the same place. this is after it prints 2,10. What I really don’t understand is when I add another empty {} to the terrainCoordinates table it changes when the error occurs. Any help appreciated.

local runService = game:GetService("RunService")

local nodeSize = 10
local perlinNoiseResolution = 150
local perlinNoiseFrequency = 5 --number of hill
local perlinNoiseAmplitude = 100

local terrainCoordinates = {{Vector3.new(1,1,1)},{},} --a two dimentional array containing the 3d positions of all nodes

local function getHight(x,z)
	local noiseHeight = math.noise(x/perlinNoiseResolution*perlinNoiseFrequency, z/perlinNoiseResolution*perlinNoiseFrequency)+0.5
	noiseHeight = math.clamp(noiseHeight,0,1)
	return noiseHeight
end

print(terrainCoordinates)
for x = 1, nodeSize do
	for z = 1, nodeSize do
		local part = Instance.new("Part")
		part.Parent = workspace:WaitForChild("GeneratedTerrain")
		part.Anchored = true
		part.Size = Vector3.new(0.5,0.5,0.5)
		
		local partHeight = getHight(x,z)
		
		part.Position = Vector3.new(x*4,partHeight*perlinNoiseAmplitude,z*4)
		
		terrainCoordinates[x][z] = part.Position
		print(x,z)
		print(terrainCoordinates)
		
	end
end

the error is
image

Your array only defines 2 indexes so when your x value is set to 3 you get your error because you only have 2 defined([1] and [2]). You could just define your terrainCoordinates = {} and then right after the for loop for x do:

terrainCoordinates[x] = {}

This should fix your issue

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