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

How would I reduce the lag from this script and make it run smoothly?

Asked by 9 years ago
local enemy = script.Parent
local pathfinding = game:GetService('PathfindingService')
local rs = game:GetService('RunService')
table.foreach(game.Workspace:GetChildren(),
    function(i,v)
        player = game.Players:FindFirstChild(v.Name)
        if player ~= nil then
            rs.Heartbeat:connect(function()
                wait(1)
                local distance = (enemy.Torso.Position-v.Torso.Position).magnitude
                if distance <= 75 and distance >= 2 then
                    searching = false
                    function moveto(path)
                        local points = path:GetPointCoordinates()
                        for _, point in pairs(points) do
                            enemy.Humanoid:MoveTo(point)
                            repeat
                                far = (enemy.Torso.Position-point).magnitude
                                wait()
                            until far <= 3
                        end
                    end
                    path = pathfinding:ComputeRawPathAsync(enemy.Torso.Position, v.Torso.Position, 500)
                    moveto(path)
                end
            end)
        end
end)

Answer this question