My AI Won't Work! Please Help!

I need help with my AI!

So I recently started working on a Horror Game project with one of my Friends, and decided to go ahead and work on the Monster.

P.S We’re doing it for fun. I don’t know if it’s going to go anywhere or not, lol

Now, I’ve never done AI before… I’ve only worked on it whenever I was following a tutorial, and even then I could never grasp how it worked.

The AI itself works. It walks from Waypoint to Waypoint without very many issues. But whenever it sees a Player, it will constantly flicker back and forth, like as if it was trying to decide whether it wanted to chase the Player or continue on with its Journey.

I’m the most Beginner of Beginner scripters, so I would love for someone to help me fix this script, and explain how they did it in the process.

If you need any information other than the script it self, than just reply to this, and I’ll try my best to get back to you.

If I can’t get the AI working then I’ll probably start from scratch.

Have a nice day! :slight_smile:

SCRIPT :

local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Enemy = script.Parent
local Humanoid = Enemy:WaitForChild("Humanoid")
Enemy.PrimaryPart:SetNetworkOwner(nil)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local WalkSpeed = Enemy:GetAttribute("WalkSpeed")
local SprintSpeed = Enemy:GetAttribute("SprintSpeed")
local Damage = Enemy:GetAttribute("Damage")

-- Functions --

local function getPath(destination)
	local path = Pathfinding:CreatePath({
		AgentHeight = 6;
		AgentRadius = 3;
		AgentCanJump = false;
		AgentCanClimb = false;

		Costs = {
			Water = 100;
			DangerZone = math.huge
		}
	})

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

	return path
end

local function findTarget()	
	local nearestTarget
	local MaxDistance = 50

	for index, player in pairs(Players:GetPlayers())do
		if player.Character then
			local target = player.Character
			local distance = (Enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < MaxDistance then
				nearestTarget = target
				MaxDistance = distance
			end
		end
	end

	return nearestTarget
end

local function Capture (target)
	local distance =(Enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position) .Magnitude

	if distance > 3 then
		Humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		target.Humanoid:TakeDamage(Damage)
	end
end

local function walkTo(destination)
	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()

			if target then
				Capture(target)
				Enemy.Humanoid.WalkSpeed = SprintSpeed
				break
			else
				Enemy.Humanoid.WalkSpeed = WalkSpeed
				Humanoid:MoveTo(waypoint.Position)
				Humanoid.MoveToFinished:Wait()
			end
		end
	else
		Humanoid:MoveTo(destination.Position - Enemy.HumanoidRootPart.CFrame.LookVector * 10)
	end
end

local function Patrol()
	local waypoints = workspace.Waypoints:GetChildren()
	local randomWaypoint = math.random(1, #waypoints)
	walkTo(waypoints[randomWaypoint])
end

-- Main Program --

while wait(0.01) do
	Patrol()
end