Is it possible to get a value from a table from a i, v ipair?

asking this because its not working for me but
i tried doing this

function Grid.CellTablePartDebug(Table)
	for i, v in ipairs(Table) do
		PartDebug(v["Position"])
	end
end

and it just keeps erroring with

ReplicatedStorage.GridGeneration:156: attempt to call a table value

Is that a module? Could you please provide the whole script?

1 Like

Try changing it from ipairs to pairs. Also provide the full script please!

2 Likes

Which line is 156 on your GridGeneration script?

It appears you are trying to pass a nested dictionary value into a function.

3 Likes

Instead of nesting Tables inside of Tables, I would suggest just creating a class and using that.

1 Like

Never mind it’s too early in the morning for me to be scripting. The problem is actually that PartDebug is a table, assuming that’s the line with the error.

Also please, in the future, highlight the line with the error so we don’t need to guess.

2 Likes
-- Variables
local NumberOfGeneratedGrids = 0
local NumberOfCellsGenerated = 0
local CellGen = require(script.CellGeneration)
local PartDebug = {}

-- Functions
local function TempDebugPart(P1, P2, P3, P4, P5)
	if #PartDebug > 0 then
		for i, v  in ipairs(PartDebug) do
			v:Destroy()
		end
	end

	local T_ = {P1, P2, P3, P4, P5}

	for i = 1, #T_ do
		local Part = Instance.new("Part")
		Part.Anchored = true
		Part.Size = Vector3.new(1,1,1)
		Part.Position = T_[i]

		local Highlight = Instance.new("Highlight", Part)
		Part.Parent = workspace
		table.insert(PartDebug, Part)
	end
end

local function RangeCheck(Position, Corner1, Corner2)
	--TempDebugPart(Position, Corner1, Corner2) 
	local Check1
	local Check2

	local PosX, PosY, PosZ = Position.X, Position.Y, Position.Z
	local Corner1X, Corner1Y, Corner1Z = Corner1.X, Corner1.Y, Corner1.Z
	local Corner2X, Corner2Y, Corner2Z = Corner2.X, Corner2.Y, Corner2.Z

	if PosX < Corner2X and PosY < Corner2Y and PosZ < Corner2Z then
		Check1 = true
	end

	if PosX > Corner1X and PosY > Corner1Y and PosZ > Corner1Z then
		Check2 = true
	end

	if Check1 and Check2 then
		return true
	end
end

-- Cells are ordered as XZY
local Grid = {}
function Grid.New(X, Y, Z, cframe, CellSize, SendDelay)
	local self = setmetatable({}, Grid)
	NumberOfGeneratedGrids += 1

	self.CFrame = cframe
	self.TotalCells = X * Z * Y
	self.NumX = X
	self.NumZ = Z
	self.NumY = Y
	self.CellSize = CellSize
	self.Cells = {}

	for x = 1, X do
		self.Cells[x] = {}
		for z = 1, Z do
			self.Cells[x][z] = {}
			for y = 1, Y do
				local WorldCFrame = cframe + (Vector3.new(x,y,z) * CellSize)
				local Cell = CellGen.New(x, y, z, WorldCFrame, CellSize)
				Cell:Display()

				NumberOfCellsGenerated += 1
				self.Cells[x][z][y] = Cell
				if SendDelay then task.wait(SendDelay) end
			end	
		end
	end

	return self, true
end

function Grid:CellFromPosition(Position)
	for x = 1, self.NumX do
		for z = 1, self.NumZ do
			for y = 1, self.NumY do
				local cell = self.Cells[x][z][y]
				local CC1 = (cell["Size"]/2 + cell.Position)
				local CC2 = (-cell["Size"]/2 + cell.Position)

				local InRange = RangeCheck(Position, CC2, CC1)

				if InRange then
					return cell
				end
			end
		end
	end

	return false
end

function Grid:GetCellsInBetween(CellA, CellB)
	local cells = {}
	local cellsTable = self.Cells
	
	local X1 = math.abs(CellA.X - CellB.X)
	local Y1 = math.abs(CellA.Y - CellB.Y)
	local Z1 = math.abs(CellA.Z - CellB.Z)
	
	local X2 = math.min(CellA.X, CellB.X)
	local Y2 = math.min(CellA.Y, CellB.Y)
	local Z2 = math.min(CellA.Z, CellB.Z)
	
	Y1 += 1
	
	for x = 1, X1 do
		for z = 1, Z1 do
			for y = 1, Y1 do
				local Cell = cellsTable[X2+x-1][Z2+z-1][Y2+y-1]
				table.insert(cells, Cell)
			end
		end
	end
	
	return cells
end

function Grid:GetCellRegionCFrame(cf)
	local halfCellSize = (self.CellSize/2)

	local CornerCFA = cf * CFrame.new(-halfCellSize.X, -halfCellSize.Y, -halfCellSize.Z)
	local CornerCFB = cf * CFrame.new(halfCellSize.X, halfCellSize.Y, halfCellSize.Z)
	
	PartDebug(cf.Position, CornerCFA.Position, CornerCFB.Position)
	
	local CellA, CellB = self:CellFromPosition(CornerCFA.Position), self:CellFromPosition(CornerCFB.Position)
	
	return self:GetCellsInBetween(CellA, CellB)
end

function Grid:GetCellRegionCells(CellA, CellB)
	return self:GetCellsInBetween(CellA, CellB)
end

function Grid.Info()
	print("Number of grids: " .. NumberOfGeneratedGrids)
	print("Number of cells: " .. NumberOfCellsGenerated)
end

function Grid.CellTablePartDebug(Table)
	for i, v in ipairs(Table) do
		PartDebug(v["Position"])
	end
end

Grid.__index = Grid
return Grid

also sorry for the late replies, i had to do something

Edit, after a complete lookover, i realized i acidentally used the table instead of trying to pass it into a function, no clue how i didnt catch this im just dumb sometimes sorry for your guy’s time

2 Likes