Hello!
I’ve got a function working for an AI path to update when the player moves according to a stored value on the closest player’s torso so it’s able to accurately pathfind towards them when they move, instead of pathfinding to their previous position before then creating a new path to their updated position, which causes the AI to be a little slow and defeats their purpose.
Now, the code DOES work, miraculously, but when the player moves a lot and the script has to update by creating a new coroutine for another path, the AI pauses in place before following that new path. Fortunately, I have a system set up when the AI is close enough to the player to ignore pathfinding and instead beeline towards the player, but getting to that step is almost impossible if the player is constantly moving.
Here’s a video of what happens: https://gyazo.com/f3cf49b1f83a59aa37b9b45c628b786e
Here’s the code for the chase and on movement:
local function chase(givenTarget)
local path = pathfindingService:CreatePath(agentParameters)
path:ComputeAsync(entityHRP.Position, givenTarget.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
local endPoint = #waypoints
_pathfindTorsoPos = Vector3.new(math.floor(waypoints[endPoint].Position.X), math.floor(waypoints[endPoint].Position.Y), math.floor(waypoints[endPoint].Position.Z))
_pfTPY = math.floor(waypoints[endPoint].Position.Y)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in waypoints do
if waypoint.Action == Enum.PathWaypointAction.Jump then
entityHumanoid.Jump = true
end
if Vector3.new(math.floor(givenTarget.HumanoidRootPart.Position.X), _pfTPY, math.floor(givenTarget.HumanoidRootPart.Position.Z)) ~= _pathfindTorsoPos then
_coYielded = true
coroutine.yield()
end
entityHumanoid:MoveTo(waypoint.Position)
entityHumanoid.MoveToFinished:Wait() -- so the entity doesnt immediately skip to the last waypoint an instead follows the path like a smartypants.
end
end
print('finished pathfind!')
_pathfindEnd = true
end
local function move()
local target, distance = findPlayerModule.findNearestTarget()
if target ~= nil then
_isRoaming = false
if distance >= _chaseDistance then
_moveRate = 0.1
if _isPathfinding == false then
_isPathfinding = true
local newChaseFunc = coroutine.create(chase)
coroutine.resume(newChaseFunc, target)
end
if _isPathfinding then
if _coYielded then
_coYielded = false
local newChaseFunc = coroutine.create(chase)
coroutine.resume(newChaseFunc, target)
end
end
else
_moveRate = 0
coroutine.close(chaseFunc)
_isPathfinding = false
entityHumanoid:MoveTo(target.HumanoidRootPart.Position) -- get em!
end
if distance <= attackDistance and target:FindFirstChildOfClass('Humanoid').Health > 0 then
attack()
end
else
_isRoaming = true
_moveRate = math.random(2,8) -- every 2-8 seconds, move to a random position. messes with locking onto target players tho because it has to wait this long. possible solution?
local randomPosX = math.random(-_roamThreshold,_roamThreshold)
local randomPosZ = math.random(-_roamThreshold,_roamThreshold)
local randomToMovePos = entityHRP.Position + Vector3.new(randomPosX, 0, randomPosZ)
entityHumanoid:MoveTo(randomToMovePos)
end
end
-- some other code is cut off but that's for detecting when to attack, dw if you see some missing ends
Please let me know if there’s any key information I’m not providing you