How to Make an AI That Patrols and Chases the Player

Hello! I am trying to create an AI that chases the player around the map. When a player is not spotted, I want them to wander around set points. I had followed a tutorial such as this, and I have viewed this similar page on the forum. The problem with this tutorial is that the AI is slow, and stutters when chasing the player (They go to where they see them and don’t change direction until they get there) and the models get stuck on walls, even after editing the script properties. I’ve tried looking it up, but that video is really the only one on the internet with pathfinding. The others just say to take a free model from the toolbox, and I would rather not do that.

Thanks,
Rocha0914 (Ro)

2 Likes

Could you send your entire script?

local monster = script.Parent
local humanoid = monster.Humanoid

local function canSeeTarget(target)
local origin = monster.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - monster.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)

local hit, pos = workspace:FindPartOnRay(ray, monster)


if hit then
	if hit:IsDescendantOf(target) then
		return true
	end
else
	return false
end

end

local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 80
local nearestTarget

for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		
		if distance < maxDistance and canSeeTarget(target) then
			nearestTarget = target
			maxDistance = distance
		end
	end
end

return nearestTarget

end

local function getPath(destination)
local PathfindingService = game:GetService(“PathfindingService”)

local pathParams = {
	["AgentHeight"] = 6,
	["AgentRadius"] = 1.5,
	["AgentCanJump"] = false
}

local path = PathfindingService:CreatePath(pathParams)

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

return path

end

local function attack(target)
local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

if distance > 5 then
	humanoid:MoveTo(target.HumanoidRootPart.Position)
else
	local attackAnim = humanoid:LoadAnimation(script.Attack)
	attackAnim:Play()
	target.Humanoid.Health = 0
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 and target.Humanoid.Health > 0 then
			print("TARGET FOUND", target.Name)
			script.Parent.Spotted.Value = true
			humanoid.WalkSpeed = 22
			attack(target)
			break
		else
			script.Parent.Spotted.Value = false
                            humanoid.WalkSpeed = 6
			print("Moving to ", waypoint.Position)
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
else
	humanoid:MoveTo(destination.Position - (monster.HumanoidRootPart.CFrame.LookVector * 10))
end

end

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

while wait(0.25) do
patrol()
end

Try replacing…

while wait(0.25) do
patrol()
end

with:

while true do
task.wait()
patrol()
end

Here is one of the problems:
robloxapp-20220307-2124302.wmv (1.1 MB)
The AI stops moving constantly and has to pause to get the player’s new location. Also, it will randomly stop seeing the player and temporarily go back to walking. (The patrol area is behind the structure)

You can also see that the AI runs into the wall, rather than pathfinding around it.

Here a pretty lengthy tutorial on YT but pretty helpful with what you want to create.

Plus he has the model already made in the description :smiley:

3 Likes