So today i’ve started working on my own and very original pathfinding system, i’ll quickly go over it;
There’s a module script called Path that handles creating path data aswell as having path functions (getting waypoints, status, etc.)
Do note that all of these ‘statuses’ are ‘custom’, and are not actually used by default;
Here’s the entire Path module (Please read, this might be very needed):
local pFindingService = game:GetService("PathfindingService")
-- Defaults;
local defaultStatuses = {
active = false,
blocked = false,
cancelled = false,
complete = false,
}
local defaultActions = {
chasing = false,
wandering = false,
fleeing = false,
distancing = false,
}
local defaultParameters = {
minimumRadius = 3,
minimumHeight = 7,
canJump = true,
canClimb = false,
waypointSpacing = 3,
costs = {}
}
local defaultTimes = {
lastActivated = 0,
lastBlocked = 0,
lastCancelled = 0,
lastComplete = 0,
}
-- Constants;
local zeroVector = Vector3.zero
local blockedTimeThreshold = 3
-- Runtime;
local path = {}
function path.New(enemy: Model, parameters: {any}?)
local newPathData = {
-- base data
user = enemy,
pathStatus = table.clone(defaultStatuses),
-- debugs
visibleWaypoints = true,
visibleRaycasts = true,
debugPrint = true,
-- positions
lastWaypointPos = zeroVector,
currentWaypointPos = zeroVector,
-- actions & pathfinding
action = table.clone(defaultActions),
pathfindingParameters = parameters or table.clone(defaultParameters),
times = table.clone(defaultTimes),
waypoints = {},
-- callbacks
onStateChanged = nil,
onBlocked = nil
}
return setmetatable(newPathData, {__index = path})
end
function path:GetWaypoints(sourcePosition: Vector3, destination: Vector3): {PathWaypoint}
if self.debugPrint then
print(`Waypoints are being computed for {self.user.Name}. Source position: {sourcePosition} | Destination: {destination}`)
end
local createdPath = nil
local succ, err = pcall(function()
createdPath = pFindingService:CreatePath(self.pathfindingParameters)
end)
if not succ then
warn(`Failed to create path for {self.user.Name}: {err}`)
return {}
end
if createdPath.Status == Enum.PathStatus.Success then
self:SetStatus('blocked', false)
self:SetStatus('active', true)
self.waypoints = createdPath:GetWaypoints()
elseif createdPath.Status == Enum.PathStatus.NoPath then
self:SetStatus('blocked', true)
self.times.lastBlocked = time()
self.waypoints = {}
end
createdPath.Blocked:Connect(function(blockIdx)
if self.debugPrint then
print(`{self.user.Name}'s path has become blocked`)
end
path:SetStatus('blocked', true)
self.times.lastBlocked = time()
end)
return self.waypoints
end
function path:GetStatus(): string
local pathStatus = self.pathStatus
for state, value in pathStatus do
if typeof(value) == 'boolean' and value == true then
return state
end
end
end
function path:SetStatus(key: string, value: boolean)
if self.pathStatus[key] ~= nil then
self.pathStatus[key] = value
if self.debugPrint then
print(`{self.user.Name}'s path has changed status: {key} has been set to {value}`)
end
else
warn(`{key} is not a valid path status.`)
end
end
function path:SetAction(key: string, value: boolean)
if self.action[key] ~= nil then
self.action[key] = value
if self.debugPrint then
print(`{self.user.Name}'s path has changed actions: {key} has been set to {value}`)
end
else
warn(`{key} is not a valid path action.`)
end
end
function path:GetLastTimeSince(key: string)
return self.times[key]
end
function path:Cancel()
self:SetStatus('active', false)
self:SetStatus('cancelled', true)
self.lastWaypointPos = zeroVector
self.currentWaypointPos = zeroVector
self.waypoints = {}
if self.debugPrint then
print(`{self.user.Name}'s path has been cancelled.`)
end
end
function path:Destroy()
local user: string = self.user.Name
for key in self do
self[key] = nil
end
print(`Path data for {user} has been destroyed.`)
end
return path
Now, the problem;
Pathfinding DOES get initiated and everything prints like usual, but the blocked status immediately goes active and breaks the while loop, not allowing the enemy to move at all:
I’m seriously not sure as to why this is happening, as i’m not setting ‘blocked’ to true anywhere.
In desperate need of help and support
Pathfinding code:
local dService = game:GetService("Debris")
-- Needed;
local enemies = workspace.Enemies
local target: Model = workspace:WaitForChild("notsad2", 5)
-- Modules;
local pathModule = require(script:WaitForChild("Path"))
local navigationModule = require(script:WaitForChild("Navigation"))
-- Tables;
local waypointActionColors = {
Walk = Color3.fromRGB(217, 0, 255),
Jump = Color3.fromRGB(255, 0, 4)
}
-- Constants;
local pathBlockedThreshold = 3
-- Helper functions;
local function CreateDebugWaypoint(at: Vector3, color: Color3)
local part = Instance.new('Part')
part.Anchored = true
part.CanCollide = false
part.CanQuery = false
part.Size = Vector3.new(1, 1, 1)
part.Transparency = 0.5
part.Position = at
part.Parent = workspace
part.Color = color
dService:AddItem(part, 3)
end
-- Main functions;
local function HandleWaypoints(enemy: Model, target: Model, pathData: {})
local enemyRoot = enemy.PrimaryPart
local targetRoot = target.PrimaryPart
local enemyHumanoid = enemy:FindFirstChildOfClass('Humanoid')
local targetHumanoid = target:FindFirstChildOfClass('Humanoid')
while pathData:GetStatus() == 'active' and enemyHumanoid.Health > 1 do
local sourcePos = enemyRoot.Position
local targetPos = targetRoot.Position
local waypoints: {PathWaypoint} = pathData:GetWaypoints(sourcePos, targetPos)
if #waypoints == 0 then
warn(`No waypoints were found for {enemy.Name}. Source: {sourcePos} | Target: {targetPos}`)
break
end
if targetHumanoid.Health < 1 then
break
end
local waypoint = waypoints[1]
pathData.currentWaypointPos = waypoint.Position
if pathData.visibleWaypoints then
local color = waypointActionColors[waypoint.Action.Name]
CreateDebugWaypoint(waypoint.Position, color)
end
if waypoint.Action == Enum.PathWaypointAction.Jump then
enemyHumanoid.Jump = true
end
enemyHumanoid:MoveTo(waypoint.Position)
enemyHumanoid.MoveToFinished:Wait()
if pathData:GetStatus() == 'blocked' and (time() - pathData.times.lastBlocked >= 3) then
pathData:Cancel()
break
end
if (targetPos - enemyRoot.Position).Magnitude < pathData.pathfindingParameters.minimumRadius then
pathData:SetStatus('complete', true)
break
end
end
pathData:Destroy()
enemy:SetAttribute('hasPathData', false)
end
local function InitiatePathfinding()
while task.wait(0.1) do
for _, enemy: Model in enemies:GetChildren() do
local hasPathData: boolean = enemy:GetAttribute('hasPathData')
if hasPathData then
warn(`Skipping {enemy.Name} since they have path data set.`)
continue
end
local pathData = pathModule.New(enemy)
local status = pathData:GetStatus()
local enemyHumanoid = enemy:FindFirstChildOfClass('Humanoid')
print(`Current status of {enemy.Name} is {status}`)
enemy:SetAttribute('hasPathData', true)
local sourcePosition = enemy.PrimaryPart.Position
local targetPosition = target.PrimaryPart.Position
local waypoints = pathData:GetWaypoints(sourcePosition, targetPosition)
pathData:SetAction('chasing', true)
pathData:SetStatus('active', true)
task.spawn(HandleWaypoints, enemy, target, pathData)
end
end
end
-- Runtime;
task.spawn(InitiatePathfinding)