I don’t know why, but for some reason my bot keeps on getting stuck in places, or waits a little bit sometimes when switching to a closer player. I am pretty new to pathfinding, so please explain to me the best you can. Here is my findPath()
function:
function findPath(target)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
MyHuman.Jump = true
findPath(target)
break
end
MyHuman:MoveTo(waypoint.Position)
local timeOut = MyHuman.MoveToFinished:Wait(1)
if not timeOut then
MyHuman.Jump = true
print("Path was too long")
findPath(target)
break
end
if checkSight(target) then
repeat
local closestTarget = findClosestTarget()
if closestTarget.Parent ~= target.Parent then
main()
return
end
MyHuman:MoveTo(target.Position)
attack(target)
wait(0.1)
if target == nil then
break
elseif target.Parent == nil then
break
end
until checkSight(target) == false or MyHuman.Health < 1 or target.Parent.Humanoid.Health < 1
break
end
if (myRoot.Position - waypoints[1].Position).Magnitude > 20 then
findPath(target)
break
end
end
end
end
He also will sometimes stutter, when walking randomly if he finds no targets, here is the part for that:
function walkRandomly()
local xRand = math.random(-50,50)
local zRand = math.random(-50,50)
local goal = myRoot.Position + Vector3.new(xRand, 0, zRand)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoints in ipairs(waypoints) do
if waypoints.Action == Enum.PathWaypointAction.Jump then
MyHuman.Jump = true
end
MyHuman:MoveTo(waypoints.Position)
local timeOut = MyHuman.MoveToFinished:Wait(1)
if not timeOut then
print("Stuck")
MyHuman.Jump = true
walkRandomly()
end
end
else
print("Path failed")
wait(1)
walkRandomly()
end
end
Sorry if I am just throwing scripts at you, if you need me to further explain anything I can.
Edit: I forgot to mention that I am cloning the AI from ServerStorage into workspace during this process and when I test him starting in workspace, he works perfectly fine.
Thanks For Reading!