Pathfinding Ai Safezone

so im trying to add a pathfinding safezone that if your in it the pathfinding monster ai just ignores you.

the ai code:

script.Parent.HumanoidRootPart:SetNetworkOwner(nil)
local monster = script.Parent
local humanoid = monster.Humanoid

local PathfindingService = game:GetService("PathfindingService")

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 = 40
	local nearestTarget = nil
	
	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 attack(target)
	local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	
	if distance > 8 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.AttackAnimation)
		attackAnim:Play()
	end
end

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 11,
		["AgentRadius"] = 4,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(monster.HumanoidRootPart.Position, destination)
	
	return path
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
				print("TARGET FOUND:", target.Name)
				attack(target)
				break
			else
				print("Moving To:", waypoint.Position)
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination - (monster.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	
	--local walkAnim = humanoid:LoadAnimation(script.walk.Animation)
	--walkAnim.Looped = true
	--walkAnim:Play()
	
	walkTo(waypoints[randomNum].Position)
end

while wait(0.3) do
	
	patrol()
end

if you could help then pls help lol

PathfindingModifiers are what you’re looking for. A detailed instructions can be found here.

To make a “safe-zone”, simply put a PathfindingModifier into a part, label it properly like “PlayerSafeZone” (Label is different from Name). Then, when creating a path, put a Costs table and assign it with math.huge (indicating it’s not traversable).

local PathfindingService = game:GetService("PathfindingService")

local path = PathfindingService:CreatePath({
    Costs = {
        PlayerSafeZone = math.huge
    }
})

You just need to add Costs in pathParams in getPath(destination) and you should be good.

1 Like

thank you lots i really needed this :smiley:

1 Like