So, i have a path finding AI that can attack an enemy if the enemy is close enough. When attacking an enemy, my AI uses Path finding so he can avoid obstacles. But there's a problem. When my AI is going to attack his enemy, he constantly walks back and forwards. Help?

**NOTE:**It's at MoveToEnemy() function (line 130-145) were my AI starts chasing the enemy.

-------------
--Variables--
-------------

local TC = nil
TC = script.Parent.Configuration.TeamColor.Value
--local pointModel = Instance.new("Model",game.Workspace)
local humanoid = script.Parent.Humanoid

-------------
--Functions--
-------------

--Finds the closest Enemy
function findClosestEnemy()
	--Variables
	local p = game.Players:GetChildren()
	local closestEnemy = nil
	local maxDistace = 50
	--Add NPCS to the players table
	for _, v in pairs (script.Parent.Parent:GetChildren()) do
		if v.Name == "Noob" or v.Name == "Guest" then
			table.insert(p,v)
		end
	end
	for _,player in pairs (p) do
		--If "player" classname is a player then
		if player.ClassName == "Player" then
			if player.TeamColor ~= BrickColor.new(script.Parent.Configuration.TeamColor.Value) then
				local char = player.Character
				if char ~= nil then
					humanoid = char:findFirstChild("Humanoid")
					if humanoid and humanoid.Health>0 then
						if closestEnemy == nil then
							closestEnemy = char
						else
							local NCEMag = (char.Torso.Position - script.Parent.Torso.Position).magnitude
							local CEMag = (closestEnemy.Torso.Position - script.Parent.Torso.Position).magnitude
							if NCEMag<CEMag then
								closestEnemy = char
							end
						end
					end
				end
			end
		--If "player" classname is not a player then
		else
			if player ~= nil then
				if player.Configuration.TeamColor ~= script.Parent.Configuration.TeamColor then
					humanoid = player:findFirstChild("Humanoid")
					if humanoid and humanoid.Health>0 then
						if closestEnemy == nil then
							closestEnemy = player
						else
							local NCEMag = (player.Torso.Position - script.Parent.Torso.Position).magnitude
							local CEMag = (closestEnemy.Torso.Position - script.Parent.Torso.Position).magnitude
							if NCEMag<CEMag then
								closestEnemy = player
							end
						end
					end
				end
			end
		end
	end
	return closestEnemy
end

--Finds the closest Enemy SpawnPoint
function findClosestEnemySpawnpoint()
	local SpawnsContainer = script.Parent.Parent:GetChildren()
	local closestSpawnPoint = nil
	for _, spawn in pairs (SpawnsContainer) do
		if spawn.ClassName == "SpawnLocation" then
			if spawn.TeamColor ~= TC then
				if closestSpawnPoint == nil then
					closestSpawnPoint = spawn
				else
					local NSPMag = (spawn.Position - script.Parent.Torso.Position).magnitude
					local CSPMag = (closestSpawnPoint.Position - script.Parent.Torso.Position).magnitude
					if NSPMag < CSPMag then
						closestSpawnPoint = spawn
					end
				end
			end
		end
	end
	return closestSpawnPoint
end

--Visualizes the path (only for testing)
function visualizePath(path)
	-- clear old path visualization
	for _, point in pairs(pointModel:GetChildren()) do
		point:Destroy()
	end
		
	-- calculate new visualization	
	for _, point in pairs(path) do
		local part = Instance.new("Part")
		part.Parent = pointModel
		part.FormFactor = Enum.FormFactor.Custom
		part.Size = Vector3.new(1,1,1)
		part.Position = point
		part.Anchored = true
		part.CanCollide = false
	end
end

--Move to SpawnPoint
function MoveToSpawnPoint(s)
	local PF = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Torso.Position,s.Position,512)
	local points = PF:GetPointCoordinates()
	local closestPoint = nil
	
	--visualizePath(points)	
	
	for i = 1,#points/2 do
		wait(0.1)
		if points[i*2] ~= nil then
			humanoid:MoveTo(points[i*2])
		end
		if (script.Parent.Torso.Position-s.Position).magnitude <3 then
			humanoid:MoveTo(s.Position,s)
		end
	end
end

--Move to Enemy
function MoveToEnemy(e)
	local PF = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Torso.Position,e.Torso.Position,512)
	local points = PF:GetPointCoordinates()
	local closestPoint = nil
	
	--visualizePath(points)	
	
	for i = 1,#points/2 do
		wait(0.1)
		if (script.Parent.Torso.Position-e.Torso.Position).magnitude > 3 and points[i*2] ~= nil then
			humanoid:MoveTo(points[i*2])
		else
			humanoid:MoveTo(e.Torso.Position,e.Torso)
		end
	end
end

-------------
--Task Area--
-------------

while wait(0.1) do
	--Finds if there is any enemy close
	local Enemy = findClosestEnemy()
	if Enemy~= nil then
		MoveToEnemy(Enemy)
	--If theres not any enemy close, then go to the spawn points
	else
		local Spawn = findClosestEnemySpawnpoint()
		MoveToSpawnPoint(Spawn)
	end
end

1 comment

0

NVM fixed