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)