Follow script not working

Hello, so, I’ve been trying to make myself a good follow script (For a NPC) that change path if the target player move or if the path get blocked.

However, after testing it, the NPC don’t move at all. No error in developer console. Nothing.

I’ve searched for any simple mistakes in my script but, can’t find anything anywhere. It’s probably something bigger I totally miss.

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local humanoid = script.Parent.Humanoid
local currentWaypointIndex = 0
local Blocked = false
local Over = false
local usepath = {}
local TempCible = nil
local Nearest = nil
function FindNearestPlayer(StartPlayer)
    local found
    local last = math.huge
    for i,v in pairs(game.Players:GetPlayers()) do
         local distance = v:DistanceFromCharacter(StartPlayer.HumanoidRootPart.Position)
         if distance < last then
              found = v
         end
         last = distance
    end
	return found
end
function Move(waypoint)
	humanoid:MoveTo(waypoint.Position)
	repeat
		if Blocked == true then
			return true
		end
	until humanoid.MoveToFinished
	local currentWaypointIndex = currentWaypointIndex + 1
	return nil
end
function WaypointMove()
	for _, waypoint in pairs(usepath) do
		if Move(waypoint) or Nearest.Character.HumanoidRootPart.Position ~= TempCible then
			return nil
		end
	end
	local Over = true
end
wait(5)
while true do
	repeat
		local Nearest = FindNearestPlayer(script.Parent)
		print(Nearest.Name)
		path:ComputeAsync(script.Parent.HumanoidRootPart.Position, Nearest.Character.HumanoidRootPart.Position)
		local usepath = path:GetWaypoints()
		local currentWaypointIndex = 0
		local TempCible = Nearest.Character.HumanoidRootPart.Position
		WaypointMove()
	until Over == true
	local Over = false
	wait()
end
path.Blocked:Connect(function(blockedWaypointIndex)
	if blockedWaypointIndex > currentWaypointIndex then
		local Blocked = true
	end
end)
1 Like

I may be mistaken but I believe humanoid:MoveTo() requires a part instance as the 2nd argument try replacing
humanoid:MoveTo(waypoint.Position)
with
humanoid:MoveTo(waypoint.Position,waypoint)

I have faced a similar issue yesterday when I tried to use a similar code to make a npc walk,adding the 2nd argument (the instance part) solved the problem for me.

1 Like

Good idea, I’ve added that but it seem there is other problems as it still don’t move.

I’ve tried fixing it by replacing this part of script:

function Move(waypoint)
	humanoid:MoveTo(waypoint.Position)
	repeat
		if Blocked == true then
			return true
		end
	until humanoid.MoveToFinished
	local currentWaypointIndex = currentWaypointIndex + 1
	return nil
end

By

function Move(waypoint)
	humanoid:MoveTo(waypoint.Position, waypoint)
	repeat
		if Blocked == true then
			return true
		end
	until WaypointFinished == true
	local currentWaypointIndex = currentWaypointIndex + 1
	WaypointFinished = false
	return nil
end

I’ve also added this at start of script to create variable

local WaypointFinished = false

And finally this at end of script

humanoid.MoveToFinished:Connect(function(value)
	if value == true then
		WaypointFinished = true
	end
end)

It still don’t work tough, still need help on this.

Update and up
Still not working, it seem that this part of the script

repeat
		local Nearest = FindNearestPlayer(script.Parent)
		print(Nearest.Name)
		path:ComputeAsync(script.Parent.HumanoidRootPart.Position, Nearest.Character.HumanoidRootPart.Position)
		local usepath = path:GetWaypoints()
		local currentWaypointIndex = 0
		local TempCible = Nearest.Character.HumanoidRootPart.Position
		WaypointMove()
	until Over == true

Don’t wait until is is over and just repeat over and over again each seconds as the print spam in logs.