AI keeps spam computing path (Solved)

I’m currently working on some sort of chase ai that is able to turn corners. Whenever he loses his vision of you, he computes a path using pathfindingservice. It works, however, it’s very glitchy. He just spam computes a path if you stand while out of his vision and it makes him stutter. Same goes for if you run around while he tries to compute to you while you run around.

Script:

script.Parent.PrimaryPart:SetNetworkOwner(nil);

local ai = script.Parent;
local aimodule = require(game:GetService("ServerScriptService"):WaitForChild("aiengine"):WaitForChild("AI"));

local pathfindingservice = game:GetService("PathfindingService");
local runservice = game:GetService("RunService");

local path : Path = pathfindingservice:CreatePath(aimodule.Params);

local clock = os.clock();

local foundtarget = false;
local recomputing = false;

local connection : RBXScriptConnection = nil;

while (true) do
	local target : Player = aimodule.GetClosestTarget();
	print(recomputing)
	if target ~= nil and target.Character ~= nil then
		foundtarget = true;
		if aimodule.CanSee(target) then
			ai:WaitForChild("Humanoid"):MoveTo(target.Character.PrimaryPart.Position);
		else
			if aimodule.CanGoFromPosition(ai.PrimaryPart.Position, target.Character.PrimaryPart.Position) then
				path:ComputeAsync(ai.PrimaryPart.Position, target.Character.PrimaryPart.Position);
				recomputing = true;
				if aimodule.DebugMode then
					aimodule.DisplayPath(path:GetWaypoints());
				end;
				task.spawn(function()
					for i, waypoint : PathWaypoint in pairs(path:GetWaypoints()) do
						ai:WaitForChild("Humanoid"):MoveTo(waypoint.Position);
						ai:WaitForChild("Humanoid").MoveToFinished:Wait();
						if aimodule.CanSee(target) then
							recomputing = false;
							break;
						end;
					end;
				end);
			end;
		end;
	end;
	task.wait();
end;

And yes, I tried doing

if not recomputing then
-- continue path

This loop is valid in Roblox.

while task.wait(); do
	local target : Player = aimodule.GetClosestTarget();
	print(recomputing)
end;

Shortenable.

		if aimodule.CanSee(target) then
			ai:WaitForChild("Humanoid"):MoveTo(target.Character.PrimaryPart.Position);
		elseif aimodule.CanGoFromPosition(ai.PrimaryPart.Position, target.Character.PrimaryPart.Position) then
			path:ComputeAsync(ai.PrimaryPart.Position, target.Character.PrimaryPart.Position);
			recomputing = true;
			if aimodule.DebugMode then
				aimodule.DisplayPath(path:GetWaypoints());
			end;
			task.spawn(function()
				for i, waypoint : PathWaypoint in pairs(path:GetWaypoints()) do
					ai:WaitForChild("Humanoid"):MoveTo(waypoint.Position);
					ai:WaitForChild("Humanoid").MoveToFinished:Wait();
					if aimodule.CanSee(target) then
						recomputing = false;
						break;
					end;
				end;
			end);
		end;
1 Like

thanks for this, however, ai dummy test guy still breaks pretty bad when trying to compute to you when lost

How about task.defer as replacement of task.spawn?

https://developer.roblox.com/en-us/api-reference/lua-docs/task

i solved it by myself, thanks for responses tho