Alright so im a bit stumped on this. So I am currently making a non humanoid pathfinding npc, However the script tends to break after a certain period of time. And its mostly for all of the NPCS, If I try to reinsert another. It breaks as well. Im a bit confused on this. Can someone tell me what im doing wrong?
-- code
local part = script.Parent
local force = script.Parent:WaitForChild("Force")
local pathfind = function(start,finish)
local path = game:GetService("PathfindingService"):ComputeRawPathAsync(start,finish,500)
return path:GetPointCoordinates()
end
while true do
wait()
local positions = pathfind(part.Position,script.Parent.Parent.Parent.Finish.Position)
local oldPos = script.Parent.Parent.Parent.Finish.Position
for _,pos in next, positions do
force.position = pos
repeat
wait()
until (part.Position - pos).magnitude < 2 or (oldPos - script.Parent.Parent.Parent.Finish.Position).magnitude > 5
script.Parent.BodyGyro.CFrame = CFrame.new(script.Parent.Position , Vector3.new(pos.X, script.Parent.Position.Y, pos.Z))
if (oldPos - script.Parent.Parent.Parent.Finish.Position).magnitude > 5 then break end
end
end