Help with tables

Hello, im trying to make A* pathfinding, and im making a grid system.

What i want to know is how i would insert the part with the coordinate of it in a table, and find it with its coordinate.

This is my current code:

local Grid = {}


function CreatePart()

 local NewPart = Instance.new("Part",game.Workspace.PartFolder)
 NewPart.Size = Vector3.new(1,1,1)
 NewPart.Anchored = true
 NewPart.CastShadow = false
 NewPart.BrickColor = BrickColor.new("Black")
 NewPart.TopSurface = Enum.SurfaceType.Smooth
 NewPart.BottomSurface = Enum.SurfaceType.Smooth 
 NewPart.Material = Enum.Material.Ground 

return NewPart
end


for X = -10,10, 2 do
 for Y = -10,10,2 do

  local Part = CreatePart()
  Part.Position = Vector3.new(X,2,Y)
  
 --This is where i need to insert part into table "Grid"  with its coordinate and be able to find it by the its coordinate.
 
 end
end
1 Like

table.insert(Grid, {Part = coordinate})

How would i search it in table with the coordinate value?

1 Like

Use dictionaries:

--inserting the value using coordinate as the key
Grid[coordinate] = Part

--retrieving the value
local found = Grid[coordinate] 
if found then 
	print("found", found)
end

Although the implementation changes depending on use case.

Am i doing it right?

for X = -10,10, 2 do
 for Y = -10,10,2 do

  local Part = CreatePart()
  Part.Position = Vector3.new(X,2,Y)

  local Coordinate = Vector2.new(X,Y)
  Grid[Coordinate] = Part
  
 end
end

print( Grid[Vector2.new(-5,5)] ) -- Prints nil
1 Like
--this may change depending on your situation, in this example we convert X, Y to a string value
local index = X.."_"..Y
Grid[index] = Part

print(Grid[index])
3 Likes

[Index value]
That’s how you can select a specific index number. it just means that the value is the selected child
of the table. I think that’s what you meant?

1 Like

Thanks for the help, that is what i meant

2 Likes