Table index is nil

This error is, really… weird,because table index is not nil… error is at SetChunkVisible()

because

ChunkModels[Name] = Model
	ChunksVisible[Name] = true
	ReverseChunks[Name] = {}

Here I am ‘declaring’ it.

local function UpdateRenderedChunks(ChunkName)
	local ChunkData = ReverseChunks[ChunkName]

	for OtherChunkName,Visible in pairs(ChunksVisible) do
		print(Visible == true) 
		if Visible == true  then
			print(OtherChunkName)
			SetChunkVisible(OtherChunkName,false)
		end
	end
	
	
	if ChunksVisible[ChunkName] ~= true then
		SetChunkVisible(ChunkName,true)
	end
end

I think error is when doing this ^
But, here

	for OtherChunkName,Visible in pairs(ChunksVisible) do
		print(Visible == true) 
		if Visible == true  then
			print(OtherChunkName)
			SetChunkVisible(OtherChunkName,false)
		end
	end
	

I’m printing ‘OtherChunkName’ and it’s printing the correct chunk, but when setting visible it gives me the error… any idea?

image

FULL CODE:

local Chunks = {
	Chunk1 = {"Chunk1"},

}

local ReverseChunks = {}
local ChunkBounds = {}
local ChunksVisible = {}
local ChunkModels = {}
local LastChunk = ""

local ChunksModel = game.Workspace
local Camera = game.Workspace.CurrentCamera
local HumanoidRootPart

for Name,ChunkTable in pairs(Chunks) do
	print(Chunks)
	local Model = ChunksModel:WaitForChild(Name)
	ChunkModels[Name] = Model
	ChunksVisible[Name] = true
	ReverseChunks[Name] = {}
	warn(Name)
	for _,ChunkId in pairs(ChunkTable) do
		ReverseChunks[Name][ChunkId] = true
		print(ChunkId)
	end
	
	local Position,Size = Model:GetBoundingBox().p,Model:GetExtentsSize()
	local PosX,PosZ = Position.X*2,Position.Z*2
	local SizeX,SizeZ = Size.X/2,Size.Z/2
	ChunkBounds[Name] = {PosX - SizeX,PosZ - SizeZ,PosX + SizeX,PosZ + SizeZ}
end

local function SetChunkVisible(ChunkName,Visible)
	ChunksVisible[ChunkName] = (Visible == true and true or nil)
	ChunkModels[ChunkName].Parent = (Visible == true and ChunksModel or nil)
end


local function UpdateRenderedChunks(ChunkName)
	local ChunkData = ReverseChunks[ChunkName]

	for OtherChunkName,Visible in pairs(ChunksVisible) do
		print(Visible == true) 
		if Visible == true  then
			print(OtherChunkName)
			SetChunkVisible(OtherChunkName,false)
		end
	end
	
	
	if ChunksVisible[ChunkName] ~= true then
		SetChunkVisible(ChunkName,true)
	end
end


local function GetCameraInChunk()
	if not HumanoidRootPart then return end
	local Position = HumanoidRootPart.Position
	local PosX,PosZ = Position.X,Position.Z
	for ChunkName,Bounds in pairs(ChunkBounds) do
		local X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
		if PosX > X1 and PosZ > Z1 and PosX < X2 and PosZ < Z2 then
			print('close')
			return ChunkName
		else
				print('far')
				UpdateRenderedChunks()
		end 
	end
end



local Player = game.Players.LocalPlayer
local function CharacterAdded(Character)
	if Character then
		HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	end
end
CharacterAdded(Player.Character)
Player.CharacterAdded:connect(CharacterAdded)

while true do
	local ChunkName = GetCameraInChunk()
	if ChunkName and ChunkName ~= LastChunk then
		LastChunk = ChunkName
		UpdateRenderedChunks(ChunkName)
	end
	wait()
end

Here
image
you are invoking UpdateRenderedChunks with no parameters, therefore it continues here
image
with ChunkName = nil, because

ChunksVisible[ChunkName] ~= true

evaluates to

nil ~= true

which is true, so finally you set a value to a

ChunksVisible[ChunkName]

with ChunkName = nil in function:

local function SetChunkVisible(ChunkName,Visible)
	ChunksVisible[ChunkName] = (Visible == true and true or nil)
	ChunkModels[ChunkName].Parent = (Visible == true and ChunksModel or nil)
end

and that’s why you get error by assigning something to a field in a table indexed by nil which is what you can not do.

3 Likes

Oh! that makes sense, lol… thank you so much, also do you consider this method good?

1 Like

I didn’t analyze the whole method, if you want someone to review it then you should explain what is going on in that and post that on #help-and-feedback:code-review.

1 Like