I am developing an AI and already made the pathfinding system using a sort of A*. the function returns a table of positions where my bot have to go in order to reach the destination. I made another function which walked through these paths using Humanoid.MoveTo, like this:
function walkPath(path)
if path and typeof(path) == "table" then
for i=1,#path do
if typeof(path[i]) == "Vector3" then
bot.Humanoid:MoveTo(path[i])
bot.Humanoid.MoveToFinished:wait()
end
end
print("finished walking path")
end
end
The problem is that MoveToFinished sometimes yield too much, making the enemy AI stop a lot of times while walking through the path, which is something that makes it a lot easier to kill him. I wanted to make it faster, so I used this loop instead of MoveToFinished
function walkPath(path)
--stopwalk = false
if path and typeof(path) == "table" then
--local recalculate = false
for i=1,#path do
local pathy = path[i]+Vector3.new(0,bot.HumanoidRootPart.Position.Y-path[i].Y,0)
print(pathy)
if typeof(path[i]) == "Vector3" then
local tries = 16
repeat
--local realpath = path[i]+Vector3.new(0,bot.HumanoidRootPart.Position.Y-path[i].Y,0)
bot.Humanoid:MoveTo(path[i])
wait()
tries = tries-1
--if stopwalk == true then tries = 0 end
--print(path[i])
print((bot.HumanoidRootPart.Position-pathy).magnitude)
if (bot.HumanoidRootPart.Position-path[i]).magnitude < 3 then tries = 0 end
until tries < 1
if (bot.HumanoidRootPart.Position-path[i]).magnitude > 3 then print("Recalculate") break end
end
--if recalculate == true then break end
end
print("finished path")
end
end
This one checks a lot of times if the npc reached the pathpoint by using magnitude and after some attempts if it still didnt get to the destination it recalculates the path. The problem on this one is that i think the loop wouldnt be healthy to the game, and I tried making the wait go to .1 but then the AI gets the same yielding problem with MoveToFinished. Is there any way to replace a MoveToFinished with something that isnt a loop and gets to the destination faster?
