Attempt to index number with number[Closed]

attempt to index nil with number

module script:

local module = {}

function module.GenerateNoiseMap(mapWidth, mapHeight, scale)
	local noiseMap = {mapWidth, mapHeight}
	
	if scale <= 0 then
		scale = 0.0001
	end
	
	for y = 0,mapHeight do
		for x = 0,mapWidth do
			local sampleX = x/scale
			local sampleY = y/scale
			
			local perlinValue = math.noise(sampleX, sampleY)
			noiseMap[x][y] = perlinValue
		end
	end
	
	return noiseMap
end

return module

Which line has the error? You just gave the error and the script.

line 16 noiseMap[x][y] = perlinValue

From your error, (I believe that) it is caused by setting something to nil, while thinking it is a number. I don’t do math much, but try changing the 0s in the for loops to 1s. I’m going off for a while, I won’t reply

Just try changing 0 to 1, in your code I see division, and since it starts from 0, it turns to 0/scale, which is 0. And ultimately that would be very broken

if I changed it to 1 then I will get this error “attempt to index number with number”

math.noise required 3 arguments, x, y and z.
(example: math.noise(5, 1, 3))

1 Like

If you try to print in console the ‘perlinValue’ it returns 0, and this index ‘0’ is unavailable in the table ‘noiseMap’. So it makes an error.

1 Like

I’m printing it in another script.

it prints if I have 1 object inside the noiseMap

Try with this:

local module = {}

function module.GenerateNoiseMap(width, height, scale)
	local noise = {}
	
	if scale <= 0 then
		scale = 0.0001
	end
	
	for y = 1, height do
		for x = 1, width do
			local perlin = math.noise(x/scale, y/scale, 0)
			noise[x] = perlin
			noise[y] = perlin
		end
	end
	
	return noise
end

return module