I Need help with my Path finding Script.
It Keeps On Lagging, After About 20 Seconds It Starts To Lag.
I Have seen other Post about this Topic but The Solutions I Have Seen are Not For My Situation
Becuase of other people having Different Scripts.
I Have tried returning In the Move Function Using A Varaible, Just Like A Debounce To Stop The Function From Going On But it didnt work. I Have also re-wrote My Whole Script
To Make Sure I Didnt Miss Any Mistakes.
----------- Script --------------
ā Varaibles
local ps = game:GetService(āPathfindingServiceā)
local human = script.Parent:WaitForChild(āHumanoidā)
local root = script.Parent:WaitForChild(āHumanoidRootPartā)
local runservice = game:GetService(āRunServiceā)
local debounce = false
local path = ps:CreatePath()
ā Find Target Function
function findtarget()
local dist = 150
local target
for i, v in pairs(workspace:GetChildren()) do
if v:FindFirstChild('Humanoid') and v.Name ~= script.Parent.Name then
if (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude < dist then
dist = (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude
target = v.HumanoidRootPart
end
end
end
return target
end
ā Move To Target Function
function moveto()
local target = findtarget()
if target then
path:ComputeAsync(root.Position, target.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for i, v in ipairs(waypoints) do
human:MoveTo(v.Position)
local finish = human.MoveToFinished:Wait()
if not finish then
break
end
end
elseif path.Status == Enum.PathStatus.NoPath then return end
end
end
ā While Loop
while wait(0.1) do
moveto()
end