PathfindingService: Parallel Luau

parallel luau is hard

so i’m trying to make a pathfinding system to where it uses parallel luau and the problem is. i have the task.desynchronize() function in the script.

my game will have around a bunch of npcs / citizens. and other threads will be happening. making frames render really slowly. so i’m using multi threading to separate the processes

but every time i use that. inside of the module. another module is a pathfinding module that actually creates the paths. and i keep getting this error

Function Path.ComputeAsync is not safe to call in parallel  -  Server

so i’m trying to see how i can make it to where it can compute the path without giving the error.
i have heard using a bindable event. or using the new actor:SendMessage function with the topic and the data.

if that’s the only way. can someone show me a example on how i will do that. some code at least.
here is the code by the way.

-- the code that executes the path
local x = math.random(-20, 20)
local z = math.random(-20, 20)
					
local pos = humanoidRootPart.Position + Vector3.new(x, 0, z)
task.desynchronize()
path:Move(pos)

-- what path:Move does
function path:Move(position)
	local model = self.Model
	
	local humanoidRootPart = model.HumanoidRootPart
	local humanoid = model.Humanoid
	
	local path: Path = self.Path
	path:ComputeAsync(humanoidRootPart.Position, position)
	
	local waypoints = path:GetWaypoints()
	for i, waypoint: PathWaypoint in ipairs(waypoints) do
		if self.Ended then
			break
		end
		
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end
		
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end
end

You can’t (thanks Roblox…?), however, the method is already asynchronous so it’s safe to assume that it shouldn’t hold up frames for much if any time at all. Basically means the computation is already (hopefully) parallel.

1 Like

I don’t believe you can get :ComputeAsync to ever work within parallel luau, as parallel luau doesn’t allow you to yield.
The more practical solution is to just task.desynchronize() after having computed the path.

Then again, it still wouldn’t work because you are yielding again within the parallel thread

so it wouldnt hold up frames anyway? so i can just not put it in a parallel luau.
cause before. once i had like 100 npcs and it just really bugged out.

taking up to 3 more seconds a frame. leading to like 2 fps.

ah, i didnt know u cannot yield.