Pathfinding AI running back and forth HELP

So im scripting ai for a npc but if the npc is between two players or more it starts to bug out heres a example

local PathfindingService = game:GetService("PathfindingService")

local MonsterHumanoid = script.Parent.Humanoid
local MonsterHumanoidRootPart = script.Parent.HumanoidRootPart

local Configuration = script.Configuration
local Detected = script.Detected
local TweenModule = require(game.ReplicatedStorage.TweenModule)

local Pathfinder = require(game.ReplicatedStorage.SimplePath)
local Path = Pathfinder.new(script.Parent)

spawn(function()
	while true do
		wait()
		for _,player in pairs(game.Players:GetPlayers()) do
			if player.Character then
				local CharacterHumanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
				if Configuration.CanChase.Value then
					if (CharacterHumanoidRootPart.Position - MonsterHumanoidRootPart.Position).Magnitude < 120 then
						--MonsterHumanoid:MoveTo(CharacterHumanoidRootPart.Position)
						Path:Run(CharacterHumanoidRootPart.Position)
						Configuration.Chasing.Value = true
					elseif (CharacterHumanoidRootPart.Position - MonsterHumanoidRootPart.Position).Magnitude > 120 then
						Configuration.Chasing.Value = false
					end
				end
			end
		end
	end
end)

spawn(function()
	Detected.Event:Connect(function()
		script.Parent.Head.Scream:Play()
		TweenModule.WalkSpeed(MonsterHumanoid, 0.75, 1.5)
		wait(5.75)
		TweenModule.WalkSpeed(MonsterHumanoid, 0.75, 17.25)
	end)
end)

The code

The pathfinding module is SimplePath - Pathfinding Module
By @GrayzcaIe

1 Like

It doing that because its trying to go to both players, so it would redo the path and go to the other player thats why, remember the npc’s brain is the script, so it would EXACTLY do what u write, wether if thats how u intentioned it to work or not.