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

My NPC pathfinding script appears to not follow the player?

Asked by 2 years ago

So I made this script to enchant my original npc following script (Because the original one would just go on a strait line

But It appears to not follow the player

local NPC = script.Parent --Describes the humanoid
local PathfindingService = game:GetService("PathfindingService") ---Makes a varibel of PathfindingService
local Players = game.Players

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        while wait() do
            local path = PathfindingService:CreatePath()
            path:ComputerAsync(NPC.Torso.Position, char.Humanoid.Torso.Position)
            local waypoints = path:GetWaypoints()

            for i, waypoints in pairs(waypoints) do
                NPC:MoveTo(waypoints.Position)
                NPC.MoveToFinish:Wait(5)
            end
        end
    end)
end)

while wait() do
    NPC:MoveTo(char.Humanoid.Torso.Position)
end

1 answer

Log in to vote
0
Answered by
SuperPuiu 497 Moderation Voter
2 years ago

Basic error, look at the comment I left in the script:

local NPC = script.Parent --Describes the humanoid
local PathfindingService = game:GetService("PathfindingService") ---Makes a varibel of PathfindingService
local Players = game.Players

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        while wait() do
            local path = PathfindingService:CreatePath()
            path:ComputerAsync(NPC.Torso.Position, char.Humanoid.Torso.Position)
            local waypoints = path:GetWaypoints()

            for i , v waypoints in pairs(waypoints) do -- forgot to add v
                NPC:MoveTo(v.Position) -- Go to v, waypoints is a table
                NPC.MoveToFinish:Wait(5)
            end
        end
    end)
end)

while wait() do
    NPC:MoveTo(char.Humanoid.Torso.Position)
end
Ad

Answer this question