Running Pathfinding in parallel

Hi i have been making an aggressive npc and i have run on a couple issues trying to use parallel lua.
Basically it says the functions like computeAsync and CreatePath are not safe, the problem is that sometimes they do work its like a random warning(its not realy an error as it doesn’t send the red text)

Last week it was working fine and now it broke, and before it was still giving those warning but it was still working fine

Keep in mind its my first time making an npc and this has gone through various iterations before even then it can still be improved.

Main function

function task.run(obj)
	local Blackboard = obj.Blackboard
	local Target = workspace.Alive:FindFirstChild(Blackboard.Target)
	local HMR = obj.Instance.HumanoidRootPart
	
	if not Target then return end 
	
	local Waypoints = obj.CreateWaypoints(obj.Instance, Target.HumanoidRootPart.Position)

	if Waypoints then
		Blackboard.MovementData.NextDestination = Waypoints[2]
		Blackboard.MovementData.WaypointIndex = 2
		Blackboard.MovementData.Waypoints = Waypoints

		obj.SharedTable.Destination = Blackboard.MovementData.NextDestination
		Blackboard.IsChasing = true
	end
	
	if ((Blackboard.MovementData.NextDestination - HMR.Position) * Vector3.new(1,0,1)).Magnitude <= Blackboard.MovementData.DestinationDistanceThreshold then
		Blackboard.MovementData.WaypointIndex += 1

		if Blackboard.MovementData.Waypoints[Blackboard.MovementData.WaypointIndex] then
			Blackboard.MovementData.NextDestination = Blackboard.MovementData.Waypoints[Blackboard.MovementData.WaypointIndex]

			obj.SharedTable.Destination = Blackboard.MovementData.NextDestination
		else
			Blackboard.IsChasing = false
			Blackboard.MovementData.NextDestination = Vector3.new(0, 0, 0)
			Blackboard.MovementData.Waypoints = {}
			Blackboard.MovementData.WaypointIndex = 2
			obj.SharedTable.Destination = nil

			return FAIL
		end

	end
	
	obj.SharedTable.AiDecision = "Chase"
	
	return SUCCESS
end

PathFinding function

local function CreateWaypoints (Character, Destination)
	local path = PathfindingService:CreatePath({
		AgentRadius = 2,
		AgentHeight = 6,
		AgentCanClimb = true,
		AgentCanJump = true,
		Costs = {
			Water = 20
		}
	})
	
	path:ComputeAsync(Character.PrimaryPart.Position, Destination)

	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		local MovementWaypoints = {}

		for i, waypoint in waypoints do
			MovementWaypoints[i] = waypoint.Position
		end

		return MovementWaypoints
	end
end
2 Likes

It could have worked last week because there were no conflicting access to non-thread-safe resources occurring during your tests but it is still a ticking bomb that could always go off. You should move the pathfinding calls out of the parallel section.

Something similar to this should help resolve this issue:

local function ComputePath(Character, Destination, callback)
    local path = PathfindingService:CreatePath({
        AgentRadius = 2,
        AgentHeight = 6,
        AgentCanClimb = true,
        AgentCanJump = true,
        Costs = {
            Water = 20
        }
    })

    local success, message = pcall(function()
        path:ComputeAsync(Character.PrimaryPart.Position, Destination)
    end)

    if success then
        if path.Status == Enum.PathStatus.Complete then
            local waypoints = path:GetWaypoints()
            local MovementWaypoints = {}

            for i, waypoint in ipairs(waypoints) do
                MovementWaypoints[i] = waypoint.Position
            end

            callback(MovementWaypoints)  -- Handle waypoints
        else
            callback(nil)  -- Handle the failure case
        end
    else
        warn(message)  -- Log the error message
        callback(nil)
    end
end
-- Function to handle the result of the pathfinding computation
local function onPathComputed(waypoints)
    if waypoints then
        -- Code to handle the waypoints
    else
        -- Code to handle failure to compute the path
    end
end

-- Example of how to use the ComputePath function with a callback
ComputePath(character, destination, onPathComputed)

1 Like

its not really a fix but thanks for the tip im moving it to Serial context. And hoping it works also idk now Humanoid:MoveTo() is not working at all but i’ll fix everything and update here.

Thanks for now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.