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

Why do my NPC's lag when they are walking after one loop through the script?

Asked by 4 years ago

I am working on NPCs to walk around my map in game, but after one iteration through the script they seem to stop for 0.5 seconds after each step. What could be causing this, and how could I fix it? Here is my script below:

Script:

local PathfindingService = game:GetService("PathfindingService")
-- Variables for the zombie, its humanoid, and destination
local zombie = script.Parent
local humanoid = zombie:WaitForChild("Humanoid")
local destinations = game.Workspace.Destinations:GetChildren()

-- Endless Loop
 while true do
    -- Create the path object
    for destination = 1,#destinations do
        local path = PathfindingService:CreatePath()
        local destination2 = destinations[destination]

    -- Compute the path
        path:ComputeAsync(zombie.HumanoidRootPart.Position, destination2.Position)

    -- Get the path waypoints
        local waypoints = path:GetWaypoints()
    -- Loop through waypoints
        for _, waypoint in pairs(waypoints) do
            --local part = Instance.new("Part")
            --part.Shape = "Ball"
            --part.Material = "Neon"
            --part.Size = Vector3.new(0.6, 0.6, 0.6)
            --part.Position = waypoint.Position
            --part.Anchored = true
            --part.CanCollide = false
            --part.Parent = game.Workspace

            -- Move zombie to the next waypoint
            humanoid:MoveTo(waypoint.Position)
            -- Wait until zombie has reached the waypoint before continuing
            humanoid.MoveToFinished:Wait()
        end
    end
    wait(4) --Wait at final destination before repeating loop
end
0
Does anyone know what the problem could be? Channy97 10 — 4y
0
Maybe put a wait(.1) in the while loop. niroqeo 123 — 4y

Answer this question