I have watched and researched some pathfinding to tutorials and I made a script that will make a NPC constantly follow the closest player, but there is an error that I cannot really fix. Here is the script:
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local part = script.Parent.HumanoidRootPart
local maxDistance = 30
while true do
wait(1)
local nearestPlayer, nearestDistance
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(part.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
local path = PathfindingService:CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position)
local waypoints = path:Getwaypoints() --Here is the error spot
for i, waypoint in pairs(waypoints) do
script.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Humanoid:Wait(2)
end
script.Parent.Humanoid:MoveTo(game.Workspace.endingPart.Position)
end
end
The error that I get is “Getwaypoints is not a valid member of Path “Instance””
I get what it means, but I need it to work there for the script to work, that is my only problem.
Please someone help!