Help with NPC path finding

So I made a NPC that can walk around the point, the problem is that NPC can only move two point (Mean there are PointA, PointB, Point C, the NPC only move PointB then return to PointA and keep repeat again.). Here my code preview:

-- Set patrol delay at 2 seconds

local PATROL_DELAY = 2

-- Variables for the zombie and its humanoid

local guy = game.Workspace.Special.Guard

local humanoid = guy.Humanoid

-- Variables for the point(s) the zombie should move between

local pointA = game.Workspace.Special.Point1

local pointB = game.Workspace.Special.Point2

local pointC = game.Workspace.Special.Point3

-- Variable to keep track of the zombie's next destination

local nextDestinationObject = pointA

-- Loop to move between the two points

while wait(PATROL_DELAY) do

-- Move the zombie to the next destination

humanoid:MoveTo(nextDestinationObject.Position)

-- Wait until the zombie has reached its target

humanoid.MoveToFinished:Wait()

-- Switch the current target to the other target

if nextDestinationObject == pointA then

nextDestinationObject = pointB

else

nextDestinationObject = pointA

end

if nextDestinationObject == pointB then

nextDestinationObject = pointC

else

nextDestinationObject = pointA

end

if nextDestinationObject == pointC then

nextDestinationObject = pointB

end

end
1 Like

maybe this could help

local PATROL_DELAY = 2

-- Variables for the zombie and its humanoid

local guy = game.Workspace.Special.Guard

local humanoid = guy.Humanoid

-- Variables for the point(s) the zombie should move between

local pointA = game.Workspace.Special.Point1

local pointB = game.Workspace.Special.Point2

local pointC = game.Workspace.Special.Point3

local DestinationObject = {pointA,pointB,pointC,pointB} -- order of DestinationObject

local i = 1
-- Variable to keep track of the zombie's next destination

local nextDestinationObject = pointA

-- Loop to move between the two points

while wait(PATROL_DELAY) do

	-- Move the zombie to the next destination

	humanoid:MoveTo(DestinationObject[i].Position)

	-- Wait until the zombie has reached its target

	humanoid.MoveToFinished:Wait()

	-- Switch the current target to the other target
	
	i = i + 1
	
	if i>#DestinationObject then
		i = 1
	end

end
2 Likes