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