I'm using PathfindingService and the line human.MoveToFinished:Wait() makes it work fine for about 30 seconds, but then the character keeps doing the pathfinding, just way too slowly, like it's lagging. If I remove that line, the issue goes away, but then the character no longer avoids obstacles or jumps. I also want it to be able to jump between platforms, but it just stays on the first one and doesn't try to jump to the next (the character is following the player). Does anyone know how to fix this?
local pathFindingS = game:GetService("PathfindingService")
local center = script.Parent:FindFirstChild("HumanoidRootPart")
local path = pathFindingS:CreatePath()
local human = script.Parent:WaitForChild("Humanoid")
human.WalkSpeed = 50
local function distance()
local agro = 100
local search = game.Workspace:GetChildren()
local target = nil
for i, v in pairs(search) do
if v:FindFirstChild("Humanoid") and v.Name ~= script.Parent.Name then
local hum = v:FindFirstChild("Humanoid")
local part = v:FindFirstChild("HumanoidRootPart")
if (center.Position-part.Position).magnitude < agro then
agro = (center.Position-part.Position).magnitude
target = part
end
end
end
return target
end
while task.wait() do
rootTarget= distance()
if rootTarget ~= nil then
local path = pathFindingS:CreatePath()
path:ComputeAsync(center.Position, rootTarget.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
print("a")
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait()
end
end
end