Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pathfinding npc not moving even though path & waypoints exist, what do I do?

Asked by 6 years ago

I've made an npc and a script to go with it which are supposed to track down the nearest player and get to them through pathfinding. However, when I try to run the script, the npc does not move at all. Here is the script:

local findNearestTorso = function()
    local torsotracked = nil
    local torsodisttemp = nil
    local torsodist = 0
    if game.Players:GetChildren() then for i,v in pairs(game.Players:GetChildren()) do
        if v.Character:FindFirstChild("HumanoidRootPart") then
            torsodisttemp = (v.Character.Torso.Position - script.Parent.Torso.Position).magnitude
            if torsodisttemp > torsodist then
                torsotracked = v.Character.Torso
                torsodist = (v.Character.Torso.Position - script.Parent.Torso.Position).magnitude
            end
        end
    end
    return(torsotracked)
end end
local table1 = {}
local pathservice = game:GetService("PathfindingService")
while wait() do
    local start = script.Parent.Torso
    local finish = findNearestTorso()
    if finish then
        local path = pathservice:FindPathAsync(start.Position,finish.Position)
        local points = path:GetWaypoints()
        for i,v in pairs(points) do
            local brick = Instance.new("Part",workspace)
            brick.Size = Vector3.new(0.3,0.3,0.3)
            brick.Anchored = true
            brick.CanCollide = false
            brick.Material = "Neon"
            brick.Color = Color3.new(1,1,1)
            brick.CFrame = CFrame.new(v.Position)
            table.insert(table1,brick)
            if v.Action == "Walk" then
                script.Parent.Humanoid.WalkToPoint = v.Position
                repeat
                    local dist = (start.Position-v.Position).magnitude
                    wait(3)
                until dist < 1
            elseif v.Action == "Jump" then
                script.Parent.Humanoid.WalkToPoint = v.Position
                repeat
                    script.Parent.Humanoid.Jump = true
                    local dist = (start.Position-v.Position).magnitude
                    wait(3)
                until dist < 1
            end
            wait()
        end
        for i,v in pairs(table1) do
            v:Destroy()
        end
    end
end

The thing with 'brick' is to visualize the path and it works, even though the npc does not move. Also, there is no error in output. What am I doing wrong?

Answer this question