AI Chasing Script Bug

Hey guys! I’m a Brazilian developer new to PathfindingService and I’m trying to make an AI Chase script for a horror game, the only problem is that the “monster” doesn’t want to dodge obstacles, like walls for example, and I’ve tried everything, but I didn’t get a right answer, can anyone help me?

The Script:

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

local pathParams = {
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = true,
	AgentCanClimb = true,
	WaypointSpacing = 2,
	Costs = {
	}
}
local path = PFS:CreatePath(pathParams)
local searchDistance = 100000

local parent = script.Parent
local human = parent.Humanoid
local hrp = parent.PrimaryPart
local head = parent.Head

local destination
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

function FindNearestChar()
	local nearestPlayer
	local nearestDistance
	for _, player in pairs(Players:GetChildren()) do
		local playerChar = player.Character
		if playerChar and playerChar.Humanoid and playerChar.Humanoid.Health > 0 then
			local mag = (player.Character.HumanoidRootPart.Position - head.Position).Magnitude
			
			if nearestDistance then
				if mag < nearestDistance then nearestDistance = mag; nearestPlayer = player.Character end
			else
				nearestDistance = mag; nearestPlayer = player.Character
			end
		end
	end
	return nearestPlayer
end

function ChasePlayer()
	local playerChar = FindNearestChar()
	
	if playerChar then
		local success, errorMessage = pcall(function()
			path:ComputeAsync(hrp.Position, playerChar.PrimaryPart.Position)
		end)
		
		if success and path.Status == Enum.PathStatus.Success then
			waypoints = path:GetWaypoints()
			
			for _, waypoint in ipairs(waypoints) do
				human:MoveTo(waypoint.Position)
			end
		else
			warn("Path not computed!", errorMessage)
		end
	end
end

RunService.Heartbeat:Connect(function()
	ChasePlayer()
end)

Yes, I know this is too simple for a smart monster, but this is as far as I can get :expressionless:

The Video:
robloxapp-20221229-1651399.wmv (2.7 MB)

You Appear to be following this Tutorial:


Make sure you follow it through 8:17 as it looks unfinished

1 Like

OMG! Thanks for the tutorial bro! I was actually trying to do this myself lol. I was following the ROBLOX Character Pathfinding tutorial. Thanks a lot for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.