Hello! I have a pathfinding script to walk the character to a circle on the map. However, I have tried making functions to determine when the character is stuck but can’t figure out how to do it. Am I doing something wrong with path.Blocked?
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
warn('blocked');
if blockedWaypointIndex >= nextWaypointIndex then
warn('redirecting.')
followPath(destination)
end
end)
local function followPath(destination)
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
warn('blocked');
if blockedWaypointIndex >= nextWaypointIndex then
warn('redirecting.')
followPath(destination)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
Module.WaypointCoroutine = coroutine.create(function()
local current = nextWaypointIndex;
humanoid:MoveTo(waypoints[nextWaypointIndex].Position);
local waitTime
waitTime = (waypoints[current].Position - waypoints[current + 1].Position).Magnitude;
waitTime = waitTime / humanoid.WalkSpeed;
waitTime = waitTime + 3;
print(waitTime)
if waitTime > 100000 or waitTime == math.huge then
waitTime = 3.100007975101471
end
task.wait(waitTime);
if current >= nextWaypointIndex then
if not Module.IsTrickOrTreating then
warn('needs to redirect...')
followPath(destination);
reachedConnection:Disconnect();
blockedConnection:Disconnect();
pcall(function()
task.cancel(Module.WaypointCoroutine)
end)
end
else
pcall(function()
task.cancel(Module.WaypointCoroutine)
end)
end
end)
task.spawn(Module.WaypointCoroutine);
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end);
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
Controls:Enable()
if reachedConnection then
reachedConnection:Disconnect()
reachedConnection = nil;
end
if blockedConnection then
blockedConnection:Disconnect();
blockedConnection = nil;
end
if ringChangedConnection then
ringChangedConnection:Disconnect()
ringChangedConnection = nil
end
pcall(function()
task.cancel(Module.WaypointCoroutine)
end)
end
end