Workspace.TestBody.Script:33: attempt to index nil with 'Status'

Not sure what’s wrong here. path variable just doesn’t work? Cant index path for some reason.

local PathfindingService = game:GetService("PathfindingService")

local Pets = require(game.ServerScriptService.Pets)
local PetStats = Pets.new(25,0.5,10)

local Pet = script.Parent
local Humanoid = Pet:WaitForChild("Humanoid")
local HumanoidRootPart = Pet.HumanoidRootPart

local AgentParams = {
	AgentHeight = 5,
	AgentCollisionGroupName = "Pets",
	AgentCanJump = true
}

local Path = PathfindingService:CreatePath()
local Goal

local GoToFood = coroutine.create(function()
		while true do
			local FoodsAvailable = workspace.FoodsAvailable:GetChildren()
			Goal = FoodsAvailable[math.random(1,#FoodsAvailable)]
			coroutine.yield()
		end
	end)

coroutine.resume(GoToFood)

local function WalkTo()
	print(HumanoidRootPart.Position, Goal.Position)
	local path = Path:ComputeAsync(HumanoidRootPart.Position, Goal.Position)
	
	if path.Status == Enum.PathStatus.Success then
		local Waypoints = path:GetWaypoints()
		
		for i,Waypoint in pairs(Waypoints) do
			if Goal == nil then
				path:Destroy()
			end
			
			if Waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			
			if Goal then
				Humanoid:MoveTo(Goal)
				Humanoid.MoveToFinished:Wait()
				coroutine.resume(GoToFood)
				break
			else
				Humanoid:MoveTo(Waypoint.Position)
				Humanoid.MoveToFinished:Wait()
			end
		end
	end
end

while true do

	WalkTo()
	wait()
end

PathfindingService:ComputeAsync() seems to be returning nil. Try checking if its nil or not

It returns nil. I thought that what it returns doesn’t matter though. The positions for both the goal and humanoidrootpart are present.

You need to remove the local part here:

local path = Path:ComputeAsync(HumanoidRootPart.Position, Goal.Position)
if path --

and just use

Path:ComputeAsync(HumanoidRootPart.Position, Goal.Position)
if Path --
1 Like

Thank you. Much appreciated. This was it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.