Pathfind script issue

I’m using a pathfinding script and trying to manipulate it to work for a game I’m working on that utilizes tile based movement. But I found a bug I can’t seem to figure out.

I’m going to be creating a path using this pathfinding script and moving the enemy to the nearest tile that leads to the player. But the tiles are not creating and I am getting this error.


(G is an IntValue that is very much present in the part.)

Here is the script I’m using:

-- ## Before running, you need two parts, "Goal" and "Start"
-- ## Put these parts into workspace and put an IntValue named "G" inside of Start.


local node_Spacing = 7

-- Required for A* pathfinding algorithm.
local open_List = {workspace:WaitForChild("DungeonPaths"):WaitForChild("Start")}
local closed_List = {}

local goal = workspace:WaitForChild("DungeonPaths"):WaitForChild("Goal")
local start = workspace:WaitForChild("DungeonPaths"):WaitForChild("Start")

local pathFind = {}

local valid_Directions = {
	Vector3.new(1, 0, 0), Vector3.new(-1, 0, 0)
	, Vector3.new(1, 0, 1), Vector3.new(-1, 0, -1)
	,Vector3.new(1, 0, -1), Vector3.new(-1, 0, 1)
	, Vector3.new(0, 0, 1), Vector3.new(0, 0, -1)
	--, Vector3.new(0, 1, 0), Vector3.new(0, -1, 0) [[Is used for vertical movement.]]
}

function create_Part(position, parent)
	local part = Instance.new("Part")
	part.Name = "Node"
	part.BrickColor = BrickColor.Blue()
	part.Position = position
	part.Size = Vector3.new(1, 1, 1)
	part.Anchored = true
	part.CanCollide = false
	part.Parent = parent

	local G = Instance.new("IntValue")
	G.Name = "G"
	G.Value = parent.G.Value + 1
	G.Parent = part

	return part
end

function cast_Ray(start, direction)
	return workspace:FindPartOnRay(Ray.new(start, direction * node_Spacing))
end

function trace_Path(final_Node)
	local current = final_Node
	repeat --wait()
		current.BrickColor = BrickColor.Yellow()
		current = current.Parent
	until current.Parent == workspace
end

function pathFind:PathFind(start, goal)
	local final = nil
	table.insert(open_List, start)
	repeat --wait()
		local low_F = math.huge
		local best = nil
		for l, node in ipairs(open_List)do
			local H = (node.Position - goal.Position).Magnitude * node_Spacing
			local G = node:WaitForChild("G").Value
			if G + H < low_F then
				low_F = G + H
				best = {node, l}
			end
		end
		table.insert(closed_List, best[1])
		if best[1] ~= start then
			best[1].BrickColor = BrickColor.Blue()
		end
		table.remove(open_List, best[2])

		for _,vector in ipairs(valid_Directions)do
			local ray_Part, ray_Pos = cast_Ray(best[1].Position, vector)
			if not ray_Part then
				local part = create_Part(ray_Pos, best[1])
				table.insert(open_List, part)
			end
		end
		final = best[1]
	until #open_List <= 0 or (best[1].Position - goal.Position).Magnitude <= node_Spacing * 1.5
	trace_Path(final)
end

return pathFind

Here is the culprit:

local G = node:WaitForChild("G").Value

Any help?

try this:

for l, node in ipairs(open_List)do
    local H = (node.Position - goal.Position).Magnitude * node_Spacing
    local G = node:FindFirstChild("G").Value
    if G + H < low_F then
        low_F = G + H
        best = {node, l}
    end
end
1 Like

Here’s the output:


And this extremely long loop above it:
image
Because I added this line for testing purposes

print(node.Parent)

In this chunk

for l, node in ipairs(open_List)do
			local H = (node.Position - goal.Position).Magnitude * node_Spacing
			print(node.Parent)
			local G = node:FindFirstChild("G").Value
			if G + H < low_F then
				low_F = G + H
				best = {node, l}
			end
		end

But alas this still isn’t working, any other ideas anybody?

Can I please have some help? I’m really struggling with this one.