I tried using pathfinding to make a character walk across a set path, but when there are more than 2 checkpoints, the character walks straight to the second checkpoint (in workspace.Path) without pathfinding, then pathfinds to the third checkpoint. Only 1 waypoint marker appeared at the second checkpoint, instead of one marker every 3 studs as expected.
I’ve also tried printing the computedPaths and it prints out 2 instances correctly, no warnings neither.
I have tried searching the devforum for similar issue but I think it’s something with my code.
local zombie = script.Parent
local waypoints = workspace.Path
local pathDiameter = 4
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath({Costs = {Grass = math.huge}, WaypointSpacing = 3, AgentRadius = pathDiameter/2 + 0.5, AgentCanJump = false})
local computedPaths = {}
sortWaypoints = function(Checkpointz)
local CPTable = {}
for i,v in pairs(Checkpointz:GetChildren()) do
if tonumber(v.Name)~=nil then
CPTable[tonumber(v.Name)] = v
end
end
return CPTable
end -- Sorts the table based on the number of the checkpoints
waypoints = sortWaypoints(waypoints)
for i, v in ipairs(waypoints) do
if i > 1 then -- Waypoint 1, or waypoint 0 is reserved for starting location
local success, errorMessage = pcall(function()
local startPos = waypoints[i-1].Position
path:ComputeAsync(startPos, v.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
table.insert(computedPaths, path)
else
warn("No valid path")
end
end
end
print(computedPaths)
local function followPath(paths)
local root = zombie.HumanoidRootPart
root:SetNetworkOwner(nil)
for i=1, #paths do
waypoints = paths[i]:GetWaypoints()
for waypoint = 1, #waypoints do
local wppart = Instance.new("Part", workspace)
wppart.Position = waypoints[waypoint].Position
wppart.Size = Vector3.new(0.5, 0.5, 0.5)
wppart.Anchored = true
wppart.CanCollide = false
wppart.Material = "Glass"
repeat
wait()
local currentWp = waypoints[waypoint]
zombie.Humanoid:MoveTo(currentWp.Position) -- Using repeated moveto functions as a walkaround
until (root.Position - Vector3.new(currentWp.Position.X, root.Position.Y, currentWp.Position.Z) ).magnitude <= 1
end
end
end
followPath(computedPaths)