Help with and error fix

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!

The error you are getting is because the method you are trying to call is spelled incorrectly. Instead of “Getwaypoints”, it should be “GetWaypoints”. So the line that is causing the error should be changed to:

local waypoints = path:GetWaypoints()

Make sure to update that line and rerun the code, it should work fine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.