NPC Trench Traversal Code Inquiry

This is the situation: NPC has spotted an enemy. To protect his trench, he needs to traverse it without jumping out on his way to the enemy.

I have made path points (P14, P13, etc.) for the NPC to follow on his way to the enemy. Upon reaching the closest path point to the enemy, the NPC simply needs to follow the enemy alone.

I am struggling to realize this through code though. This is sort of the idea I was going for:

local pathway = workspace.Pathway

local function findNearestPoint(position) -- Used to find the closest pathpoint...

	local closestPoint, closestMag = nil, nil

	for i,point in pairs(pathway:GetChildren()) do

		local mag = (position - point.Position).Magnitude
		if closestMag == nil or mag < closestMag then -- If the closestMag hasn't been declared yet, or if the new mag is THE closest one so far... 
			closestPoint, closestMag = point, mag


	return closestPoint -- return the closest point!

local start, finish = findNearestPoint(NPC_Position), findNearestPoint(Enemy_Position) -- Find the closest points to the NPC Position and the Enemy Position...

-- since pathpoints are named in a chronological order, loop runs through points from start to finish.
for count = string.gsub(start, "P", ""), string.gsub(finish, "P", "") do

	local point = pathway["P"..count]



Any help is HIGHLY appreciated! Thank you in advance!

Roblox actually just added a new feature called PathfindingModifiers (API Reference) for this exact purpose

I would give you some advice on how to use this but personally I have not tried to use it yet, but I think this DevForum post by a Roblox employee when they first released it will help you: New Pathfinding Modifiers

Best of luck, looks like an interesting game!

Thanks for the advice, kind person!

I have tried using them before without much progress, but I attribute that to my lack of research on the topic.

I will try again, this time with seriousness. You will be informed!
Also thank you very much!

1 Like