Issues with terrain generation

I’m working on some terrain generation, and I’m doing a grid based one to have way bigger worlds. So far it works, and it generates one row. But generating the next row leads to this error:
image

Here is the code (Area of line 33):

for cz=1,WorldSize do
		local startX = ((cx*size)-size)*voxelSize
		local startZ = ((cz*size)-size)*voxelSize
		--Basemap--
		for x=1,size do
			for z=1,size do
				x+= -cx+1 --(cx*size)-size-cx+1 --(cx*size)-(cx*size)-cx+1
				z+=(cz*size)-size-cz+1
				if x >= 300 or x <= 0 then
					print("Uh oh!")
					print(x)
				end
				local y = HeightMap[x][z]*amplitude
				terrain:FillBlock(CFrame.new(startX+x-(size*cx)*voxelSize,y*voxelSize,startZ+z-(size*cz)*voxelSize),Vector3.new(voxelSize,voxelSize,voxelSize),Enum.Material.Rock)
			end
			run.Heartbeat:Wait()
		end

Please help!

5 Likes

I dont really know what the fix is but the error is that it tries to index a number that doesn’t exist.

2 Likes

Which line is line 33?
That would make it easier to see whats happening.
Just from looking though, I think maybe your heightmap for [x] is nil, and so its not able to index it by [z]

2 Likes

Not enough here to help you … however this part seems a bit odd.

1 Like

The error is from this line:

(assuming it’s on the lines you gave)

The error means that either HeightMap (unlikely) or HeightMap[x] is nil, but you try to index it (it’s basically like trying to run nil[x] or nil[z]).

The reason your code gets this error is either because the HeightMap table is made incorrectly and doesn’t have a value for all the x’s you want, or because your loop goes past the bounds of the list (aka your for loop goes too far).

It looks like you have an if statement to prevent this, try also adding these lines to check what numbers are causing the problem:

for cz=1,WorldSize do
		local startX = ((cx*size)-size)*voxelSize
		local startZ = ((cz*size)-size)*voxelSize
		--Basemap--
		for x=1,size do
			for z=1,size do
				x+= -cx+1 --(cx*size)-size-cx+1 --(cx*size)-(cx*size)-cx+1
				z+=(cz*size)-size-cz+1
				if x >= 300 or x <= 0 then
					print("Uh oh!")
					print(x)
				end

				-- Make sure this can't happen!
				assert(HeightMap, "HeightMap doesn't exist! x is "..x.."!")
				assert(HeightMap[x], "HeightMap[x] doesn't exist! x is "..x.."!")

				local y = HeightMap[x][z]*amplitude
				terrain:FillBlock(CFrame.new(startX+x-(size*cx)*voxelSize,y*voxelSize,startZ+z-(size*cz)*voxelSize),Vector3.new(voxelSize,voxelSize,voxelSize),Enum.Material.Rock)
			end
			run.Heartbeat:Wait()
		end

Also the error could be from terrain:FillBlock, occasionally the engine functions throw errors like that. That would be from the inputs being formated incorrectly.

A table has an index for every value, and nil for the rest.

local myFourFavouriteFoods = {"Beer","Ale","Lager"}

for i = 1,4 do -- Run the loop 4x, because we have 4 favourite foods
	print(myFourFavouriteFoods[i])
end
-- Output:

-- Beer
-- Ale
-- Lager
-- nil, because myFourFavouriteFoods[4] is nil.

Your table has no value for the index you’re … indexing? Not sure if that’s the verb.
Indexing a table with a number for which it has no value is fine, that’ll just result in nil.
But if you then assume there should be another table there, and you index that one, then you get nil[index], and it doesn’t like that (because nil = nil, not a table).

Easy fix: if you know the size of the grid, just make sure you create data for it.

local size = 5
local grid = {}
for x = 1,size do 
	table.insert(grid,table.create(size,{})) -- for size, insert a new row into the grid, with length size populated with {}
end 

print(grid)

        [1] =  ▼  {
                       [1] = {},
                       [2] = {},
                       [3] = {},
                       [4] = {},
                       [5] = {}
                    },
                    [2] =  ▼  {
                       [1] = {},
                       [2] = {},
                       [3] = {},
                       [4] = {},
                       [5] = {}
                    },
                    [3] =  ▼  {
                       [1] = {},
                       [2] = {},
                       [3] = {},
                       [4] = {},
                       [5] = {}
                    },
                    [4] =  ▼  {
                       [1] = {},
                       [2] = {},
                       [3] = {},
                       [4] = {},
                       [5] = {}
                    },
                    [5] =  ▼  {
                       [1] = {},
                       [2] = {},
                       [3] = {},
                       [4] = {},
                       [5] = {}
                    }

Just had to move the x+= -cx+1 into the for x=1,size do area

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