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?