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

Pathfinding sometimes works, but It just randomly breaks, why Is that?

Asked by 1 year ago

Hello! So Its been a long time since I've talked about Pathfinding, so anyway, I decided to edit with the pathfinding I did and I found a random problem, basically for some whatever reason, the script would randomly stop working, meaning the NPC won't chase the player(s). However sometimes It does work, however It will randomly break and once It breaks It literally becomes Impossible for the NPC to chase the player(s) as Its pathfinding script just randomly broke

Note: The edit that I did was that I removed the NPC.Humanoid.MoveToFinish code so that way It can go chase players without having to go to the original player spot (For example, If the player moves lets say, 20 studs while the npc Is chasing them, the npc must need to go to there original spot so they can change locations)

Script:

local NPC = script.Parent
local PathfindingService = game:GetService('PathfindingService')
local Players = game:GetService("Players")
local player

game.Players.PlayerAdded:Connect(function(client)
    player = client
end)

repeat wait() until player ~= nil

local path = PathfindingService:CreatePath()

while wait() do
    local path = PathfindingService:CreatePath()

    path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)

    if path.Status == Enum.PathStatus.Success then
        local Positions = path:GetWaypoints()

        for i,v in pairs(Positions) do
            local ball = Instance.new("Part", workspace)

            ball.Position = v.Position
            ball.Anchored = true
            ball.CanCollide = false
            ball.Size = Vector3.new(4,4,4)
            ball.Transparency = 0
            ball.Shape = Enum.PartType.Ball

            NPC.Humanoid:MoveTo(ball.Position)
        end
    end
end

Answer this question