# How can I fix this

Please don’t get mad at me for creating a similar topic, but I had an issue with a script a while back that got solved, but I discovered another issue which I have been trying to solve for god knows how long. And I am dying to get this issue fixed, so I can move on.

Here it is:

local chunks = {}

local function chunkExists(chunkX, chunkZ, chunkY) -- Check's if a chunk exists
spawn(function()
local isNil = false

-- if not chunks[chunkX] then
--    chunks[chunkX] = {}

-- end
if not chunks[chunkX] then
chunks[chunkX] = {}
--  return false
end
if not chunks[chunkX][chunkZ] then
chunks[chunkX][chunkZ] = {}
--  return false
end
if not chunks[chunkX][chunkZ][chunkY] then
chunks[chunkX][chunkZ][chunkY] = {}
--return false
end

-- if isNil == false then
return chunks[chunkX][chunkZ][chunkY]
-- end
end)
end

local function roundTo(number, roundToNumber) -- Give it a value and it'll round it to the target's multiple, e.g 12 goes to 8, 17 goes to 16.
return number - (number % roundToNumber)
end

local function CheckPos(X, Z, Y)
spawn(function() -- We use spawn function because if we don't then every time this function is called then the (all) the client(s) that are running the game will freeze
local RoundedX = roundTo(X, 32)
local RoundedZ = roundTo(Z, 32)
local RoundedY = roundTo(Y, 32)

--print(" X: "..RoundedX.." Z: "..RoundedZ.." Y: "..RoundedY)
--print(RoundedZ)
--print(RoundedY)

for x = -32, 32 do
for z = -32, 32 do
for y = -32, 32 do
local cx = RoundedX + x
local cz = RoundedZ + z
local cy = RoundedY + y

if not chunkExists(cx, cz, cy) == true then
--print("x: "..cx.." z: "..cz.." y: "..cy)
chunks[cx][cz][cy] = true -- This is where I am getting the error at :/

end
end
end
end
end)
end

spawn(function()
while true do
wait(1)
--print("hh")
for i, v in pairs(game:GetService("Players"):GetChildren()) do
local C = nil
local HRP = nil

pcall(function()
if v.Character then
C = v.Character
else
C = nil
end
end)

if C ~= nil then
pcall(function()
if C:FindFirstChild("HumanoidRootPart") then
HRP = C:FindFirstChild("HumanoidRootPart")

else
HRP = nil
end
end)
end

if HRP ~= nil then
CheckPos(HRP.Position.X, HRP.Position.Z, HRP.Position.Y)
end
end
end
end)

The error is at line 53 “Attempt to index a nil value”. There is nothing nil there. I am trying to set a value that is ‘nil’ to true. The error might lie in the “ChunkExists” function, but I am not entirely sure.

This is where the error is at:

chunks[cx][cz][cy] = true -- This is where I am getting the error at >:(

I am not indexing a ‘nil’ value. chunks which is a table and is not nil, cx, cz, cy, are not nil either. What can I do to fix this?

For those who are wondering what I am trying to accomplish, is I created a 3D Perlin noise script a while back, and I want to use it in a game, where terrain generates based of the player’s position, while I was in the middle of writing this script I found this error, including another one (which got fixed in a different topic).

Without reading the entire code, I can tell you that your chunkExists function looks a bit strange: it always returns nil. It looks like you want to use the return value of that function on line 51 (if not chunkExists(cx, cz, cy) == true then), but you wrap the function in a spawn, so the function always returns nil (the function chunkExists never uses the return value of the function inside the spawn). I don’t know if this is the cause of your bug but I hope that helps.

1 Like

AHA! Let me see if wrapping the “Chunk Exists” function with a spawn as well fixes things up. Or I will remove the spawns entirely. The reason I have a spawn in there is because I had an issue a while back where the “CheckPos” function would cause my game to freeze every time it would run.

Looks like removing the spawns fixed the issue. But I want to know if there is anything that I scripted wrong in the “Chunk Exists” function. Also the game DOES freeze any time the “CheckPos” function runs… so gotta fix that up to.