Pathfinding not following player and not pathfinding very well

For some reason the first thing is that the ai no longer follows the player, instead its going in the opposite direction or to something else which is strange. Then the pathfinding isnt really doing its job very well, it doesnt go through doors and instead wants to go and run into the wall. Thank you so much if you find a solution to this!

local AIModule = {}

function AIModule.ActivateAI()
	--// V2 DadAI

	local Dad = game.Workspace.Jumpscare.Dad
	local human = Dad.Humanoid
	--// anim id 18193963545
	local pathFindingService = game:GetService("PathfindingService")
	local runService = game:GetService("RunService")

	Dad.PrimaryPart:SetNetworkOwner(nil)

	local function findTarget()
		local players = game:GetService("Players"):GetPlayers()
		local nearesttarget
		local maxDistance = 5000 -- distance

		for i,player in pairs(players) do
			if player.Character then
				local target = player.Character
				local distance = (Dad.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

				if distance < maxDistance then
					nearesttarget = target
					maxDistance = distance
				end

			end
		end
		return nearesttarget
	end

	local function getPath(destination)
		local path = pathFindingService:CreatePath()

		path:ComputeAsync(Dad.HumanoidRootPart.Position, destination)

		return path
	end

	local function pathFindTo(destination)
		local path = getPath(destination)
		local target = findTarget()

		if target and target.Humanoid.Health > 0 then
			for i,waypoint in pairs(path:GetWaypoints()) do

				if waypoint.Action == Enum.PathWaypointAction.Jump then
					human.Jump =true
				end

				human:MoveTo(waypoint.Position)
				human.MoveToFinished:Wait()

			end
		end
	end

	while	runService.Heartbeat:Wait() do
		local target = findTarget()

		if target then
			pathFindTo(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 7))
		end
	end
end

function AIModule.ResetDad()
	local Dad = game.Workspace.Jumpscare.Dad
	Dad.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(60.03, 3.25, 1.54))
end

return AIModule
2 Likes