My Pathfinding does not work

Line 44 it says "Attempt to index nil with ‘Complete’ ". Is there any idea why? Here is the line of code that is erroring:

repeat wait() until Path.Status == SimplePath.Status.Complete

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)

local RunService = game:GetService("RunService")

local Dummy = workspace.NPC

local Goal = Instance.new("Part")
Goal.Anchored = true
Goal.CanCollide = false
Goal.Size = Vector3.new(5, 5, 5)
Goal.Transparency = 1
Goal.Parent = workspace

local Path = SimplePath.new(Dummy)

Path.Visualize = true

while true do
	local closestPlayer
	local closestDistance = math.huge

	for _, player in pairs(game.Players:GetPlayers()) do
		local playerRoot = player.Character and player.Character:FindFirstChild("HumanoidRootPart")

		if playerRoot then
			local distance = (playerRoot.Position - Dummy.HumanoidRootPart.Position).Magnitude

			if distance <= 25 and distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end
	end

	if closestPlayer then
		Goal.Position = closestPlayer.Character.HumanoidRootPart.Position
		Path:Run(Goal)

		-- set network ownership to the closest player
		Dummy.HumanoidRootPart:SetNetworkOwner(closestPlayer)

		-- wait for the path to finish
		repeat wait() until Path.Status == SimplePath.Status.Complete
	else
		-- remove network ownership
		Dummy.HumanoidRootPart:SetNetworkOwner(nil)
	end

	-- wait for the next frame
	RunService.Stepped:Wait()
end

I think what you really want is to check Idle. SimplePath.Status doesn’t exist and will return nil. Try:

repeat task.wait() until Path.Status == "Idle"
1 Like

while Path and Path.Status ~= SimplePath.Status.Complete do
wait()
end

This will ensure that the loop only runs while the Path variable is not nil, and only calls the Complete property if the Path variable is not nil.

1 Like