NPC/Waiting Issues

PathfindingModule.Move(npc, workspace.Part.Position)
print("passed")
---MODULE
PathfindingModule.Move = function(npc, pos)
    local walkAnim = npc.Humanoid:LoadAnimation(npc.Animate.walk.WalkAnim)
    local idleAnim = npc.Humanoid:LoadAnimation(npc.Animate.idle.Animation1)
    local path = PathfindingService:CreatePath()
    local connecting
    path:ComputeAsync(npc.HumanoidRootPart.Position, pos)
    
    for _, waypoint in pairs(path:GetWaypoints()) do
        local part = Instance.new("Part")
        part.Size = Vector3.new(0.6, 0.6, 0.6)
        part.Transparency = 1
        part.Position = waypoint.Position
        part.Anchored = true
        part.CanCollide = false
        part.Parent = game.Workspace
        npc.Humanoid:MoveTo(waypoint.Position)
        npc.Humanoid.MoveToFinished:Wait()
        
        connecting = npc.Humanoid.MoveToFinished:Connect(function(reached)
            wait(3)
            connecting:Disconnect()
        end)
        
        npc.Humanoid.Running:Connect(function(speed)
            if speed < 1 then
                walkAnim:Stop()
                idleAnim:Play()
            else
                if speed > 4 then
                    idleAnim:Stop()
                    walkAnim:Play()
                end
            end
        end)
    end
end

The top line of code makes it so it waits until the npc reaches part.position then it executes code after that line,

So for example “passed” wouldn’t print right now until NPC reaches destination. I don’t want that

(I can’t remove npc.Humanoid.MoveToFinished:Wait() line since it breaks npcs, they stop before reaching their destination, don’t know why)

You could call the Move function in a separate thread.

spawn(function()
	PathfindingModule.Move(npc, workspace.Part.Position)
end)
print("passed"

Or

coroutine.wrap(function()
	PathfindingModule.Move(npc, workspace.Part.Position)
end)()
print("passed")

Or

local thread = coroutine.create(PathfindingModule.Move)
coroutine.resume(thread, npc, workspace.Part.Position)
print("passed")
1 Like

This isn’t related to the issue, since it’s been solved already, but I do notice a potential memory leak. You create a variable called “connecting” before the for loop, and inside the for loop you disconnect it.

First of all, each iteration of the loop would override the “connecting” variable so previous connections will never be disconnected and build up over time. Second of all, why is it even there if all you do is disconnect it right after?

connecting = npc.Humanoid.MoveToFinished:Connect(function(reached)
    wait(3)
    connecting:Disconnect()
end)

What’s the point in this?

Got told by a friend I should do this as it would avoid memory leaks, should I replace it with something else or just remove it?

Ironically the way you implemented it would most likely cause memory leaks as only 1/n waypoints are disconnected for each time the function is called. In your case it’s better to remove it, you don’t have to connect to that event since you’re doing npc.Humanoid.MoveToFinished:Wait() already.