Why my pathfinding script doesn't work?

Hi !
I’ve recently made a pathfinding script for an AI and when I try to print the PathWaypoints table that print an empty table !

local PathfindingService = game:GetService("PathfindingService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local WalkSpeed = Humanoid.WalkSpeed
local Left = workspace.Left
local Right = workspace.Right
local Area = Character.Area
local Target = false
local LeftRight = nil
local Path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = false
})

Area.Touched:Connect(function(OtherPart)
	if OtherPart.Name ~= "Area" then
		local Enemy = OtherPart.Parent
		if Enemy.Name == "1" and Character.Name == "0" and not Target then
			local EnemyHumanoid = Enemy.Humanoid
			local EnemyWalkSpeed = EnemyHumanoid.WalkSpeed
			if WalkSpeed > EnemyWalkSpeed then
				Humanoid:MoveTo(Character.PrimaryPart.Position:lerp(Enemy.PrimaryPart.Position, (WalkSpeed - EnemyWalkSpeed)/WalkSpeed))
				Target = true
				wait(3)
				Enemy:Destroy()
				Target = false
				LeftRight = nil
			else
				Humanoid:MoveTo(Character.PrimaryPart.Position:lerp(Enemy.PrimaryPart.Position, WalkSpeed/EnemyWalkSpeed))
				Target = true
				wait(3)
				Enemy:Destroy()
				Target = false
				LeftRight = nil
			end
		elseif Enemy.Name == "0" and Character.Name == "1" and not Target then
			local EnemyHumanoid = Enemy.Humanoid
			local EnemyWalkSpeed = EnemyHumanoid.WalkSpeed
			if WalkSpeed > EnemyWalkSpeed then
				Humanoid:MoveTo(Character.PrimaryPart.Position:lerp(Enemy.PrimaryPart.Position, (WalkSpeed - EnemyWalkSpeed)/WalkSpeed))
				Target = true
				wait(3)
				Enemy:Destroy()
				Target = false
				LeftRight = nil
			else
				Humanoid:MoveTo(Character.PrimaryPart.Position:lerp(Enemy.PrimaryPart.Position, WalkSpeed/EnemyWalkSpeed))
				Target = true
				wait(3)
				Enemy:Destroy()
				Target = false
				LeftRight = nil
			end
		end
	end
end)

while wait() do
	local CharacterPosition = Character.PrimaryPart.Position
	local LeftPosition = Left.Position
	local RightPosition = Right.Position
	local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
	local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
	if not Target then
		if not LeftRight and LeftMagnitude < RightMagnitude then
			Path:ComputeAsync(CharacterPosition, LeftPosition)
			local PathWaypoints = Path:GetWaypoints()
			print(PathWaypoints) --HERE
			for v, Waypoint in pairs(PathWaypoints) do
				Humanoid:MoveTo(Waypoint.Position)
				Humanoid.MoveToFinished:Wait()
			end
			LeftRight = true
		elseif LeftRight and LeftMagnitude > RightMagnitude then
			Path:ComputeAsync(CharacterPosition, RightPosition)
			local PathWaypoints = Path:GetWaypoints()
			print(PathWaypoints) --HERE
			for v, Waypoint in pairs(PathWaypoints) do
				Humanoid:MoveTo(Waypoint.Position)
				Humanoid.MoveToFinished:Wait()
			end
			LeftRight = false
		elseif LeftRight == nil then
			if LeftMagnitude < RightMagnitude then
				Path:ComputeAsync(CharacterPosition, LeftPosition)
				local PathWaypoints = Path:GetWaypoints()
				print(PathWaypoints) --HERE
				for v, Waypoint in pairs(PathWaypoints) do
					Humanoid:MoveTo(Waypoint.Position)
					Humanoid.MoveToFinished:Wait()
				end
				LeftRight = true
			else
				Path:ComputeAsync(CharacterPosition, RightPosition)
				local PathWaypoints = Path:GetWaypoints()
				print(PathWaypoints) --HERE
				for v, Waypoint in pairs(PathWaypoints) do
					Humanoid:MoveTo(Waypoint.Position)
					Humanoid.MoveToFinished:Wait()
				end
				LeftRight = false
			end
		end
	end
end
2 Likes

Any help would be appreciated !
Thanks you for the reply and have a nice day !

Nevermind I’ve solved the issue, the issue was the part height was too important !

So say how you did and mark your post that solve the problem as the solution, like this the nexts with the same problem will find the solution.

1 Like

I’ve said what’s the issue in the last message !

1 Like

Ok, sorry I missed it. Have a nice day!

1 Like

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