Now, I made a pathfinding script for an AI and I only want it to walk on a specific part (sidewalk) instead of zigzagging to its destination. How can I make this happen?

local aiFolder = workspace:WaitForChild("AI")
local module = require(game.ReplicatedStorage.DataHandler)
local pf = game:GetService("PathfindingService")
local AI = script.Parent

local function GetAI()
	return(module.GetAI(AI))
end

local AIData = GetAI(AI)

local function GiveNewDestination()
	return(module.GiveNewDestination(AI))
end

while wait() do
	pcall(function()
		local function RecalculateMovement()
			local computedPath = pf:ComputeRawPathAsync(AI.HumanoidRootPart.Position, AIData.LockedPoint.Position, math.huge)
			local points = computedPath:GetPointCoordinates()
			local closestPoint, closestIndex = nil, nil
			for x,d in pairs(points) do
				if (closestPoint == nil or ((d - AI:FindFirstChild("HumanoidRootPart").Position).magnitude < (closestPoint - AI:FindFirstChild("HumanoidRootPart").Position).magnitude)) and x ~= 1 then
					closestPoint = d
					closestIndex = x
				end
			end
			AI.Humanoid:MoveTo(points[closestIndex])
			if (AI:FindFirstChild("HumanoidRootPart").Position-AIData.LockedPoint.Position).magnitude <= 5 then
				AIData.CurrentPoint = AIData.LockedPoint
				GiveNewDestination(AI)
			end
		end
		RecalculateMovement()
	end)
end

3 comments

0

Maybe try adding something like if closestPoint.Name == "Sidewalk" or something. I am unsure about it though

1

Why use pathfinding in this scenario if you have a clear path already? Pathfinding finds a path. You already have a path.

0

I want it to find a path but only being allowed to find the path on parts named "Sidewalk"