How to stop attempting to MoveTo after a certain time

I have a pathfinding system for my NPCs. However, sometimes they may get stuck in boxes and ect therefore I have to make the system stop attempting to head in that direction and instead recompute.
Here is my original code:

for i,v in pairs(t:GetWaypoints()) do
    script.Parent:MoveTo(v.Position)
    if v.Action == Enum.PathWaypointAction.Jump then
        script.Parent:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    script.Parent.MoveToFinished:Wait()
    print("Moving onto the next checkpoint")
end

I decided to make a bindable event so that I can choose when to decide when the humanoid is done moving:

local walkToDone = Instance.new("BindableEvent")
script.Parent.MoveToFinished:Connect(function()
    walkToDone:Fire()
end)

for i,v in pairs(t:GetWaypoints()) do
    script.Parent:MoveTo(v.Position)
    if v.Action == Enum.PathWaypointAction.Jump then
        script.Parent:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    walkToDone.Event:Wait()
    print("Moving onto the next checkpoint")
end

-- Therefore with this method I can choose if I want to move on to the next checkpoint.
walkToDone:Fire()

Now onto my question:
How can I use the implementation of tick to see how long has passed before giving up on moving to that certain point?
I tried:

local walkToDone = Instance.new("BindableEvent")
script.Parent.MoveToFinished:Connect(function()
    walkToDone:Fire()
end)

for i,v in pairs(t:GetWaypoints()) do
    local time = tick()
    script.Parent:MoveTo(v.Position)
    if v.Action == Enum.PathWaypointAction.Jump then
        script.Parent:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    coroutine.resume(coroutine.create(function()
        repeat wait() until tick() - time > 5 -- wait until 5 seconds has passed
        walkToDone:Fire()
    end))
    walkToDone.Event:Wait()
    print("Moving onto the next checkpoint")
end

However the above doesn’t seem to work. I cannot describe the problem because I personally don’t understand myself.
Any suggestions?

2 Likes

You didn’t Parent the BindableEvent.

I don’t have to. That is not the issue anyway as it works completely fine. Please read the question thoroughly.

The .MoveToEnded(Or something like that) has an 8-7 seconds before firing false(If the character is blocked because of an part). The only way to change the 8-7 seconds is by making your own Event

That doesn’t seem to be an event of humanoid. I can’t find it. Mind linking it?

MoveToFinished (I always get it wrong)

Oh I’ve already used that in the post above. That is not really what I am looking for.

Thats why I said you should make a custom one(Like RunService, and check for how much time the Character din’t move so if it is bigger than you want it will fire an Event)