Is MOST of the task and coroutine library useless?

coroutines fire code alongside a thread, task.spawn does it with the engine sheduler.

1 Like

I will say that the task library’s methods are a better (more optimized) replacement of the legacy wait, spawn, and delay methods, and it also contains task.desynchronize and task.synchronize methods, which allow you to run your code on multiple cpu cores as opposed to just one (which is the default)

Parallel Luau

1 Like

i noticed some people on this thread are running with the misconception that corotiunes actually run simultaneously, but that isn’t the case

corotiunes are done via cooperative multitasking, each “thread” works together (via yielding, which can be done by task.wait , coroutine.yield, or other functions that supports yielding the thread) in order to create an illusion that multiple things are being done at the same time on one thread

1 Like

Is MOST of the task and coroutine library useless?

No. All of them are very useful, with the task library being so similar in nature with the coroutine library with threads that I would say they are both interchangeable for most cases with a few differences.

There are a lot of posts and documentation describing both the task library and coroutine library, as well as the differences between them, that I would recommend checking them out. Posting every pros, cons, and differences would just be a repeat of what other people in the past have said. I would also highly suggest you reading up on deferred behavior and how it differs from immediate behavior (there’s even an official post about this somewhere) as they are important when it comes to Roblox’s game engine behavior and race conditions.

In terms of use cases, these are extremely vital to some systems to write the most efficient and bug-free code possible, you probably just haven’t seem them yet. Here are some examples of mine utilizing threads:

Slow falling
Seat timed destroy
Queue system and yielding behavior (the module I linked also uses deferring which they explain inside the code itself)

but i use task.spawn() in my pathfinding code :frowning:

heartbeatConnection = runService.Heartbeat:Connect(function()
	if #enemiesFolder:GetChildren() > 0 then
		local enemies = enemiesFolder:GetChildren()

		for _, enemy in ipairs(enemies) do
			if enemy.EnemyType.Value == "MELEE" then
				local detectionBox = enemy:FindFirstChild("DetectionPart")
				local humanoid = enemy:FindFirstChildOfClass("Humanoid")


				local closestPlayer = nil
				local LOS = workspace:GetPartBoundsInBox(detectionBox.CFrame, detectionBox.Size)

				for _, v in pairs(LOS) do
					if v.Parent:FindFirstChild("Humanoid") then
						local targetPlayer = game.Players:GetPlayerFromCharacter(v.Parent)

						if targetPlayer then
							if closestPlayer == nil or (enemy.HumanoidRootPart.Position - targetPlayer.Character.HumanoidRootPart.Position).Magnitude < (enemy.HumanoidRootPart.Position - closestPlayer.Character.HumanoidRootPart.Position).Magnitude then
								closestPlayer = targetPlayer
							end
						end
					end
				end

				task.spawn(function()
					if closestPlayer then
						local targetPosition = closestPlayer.Character.Torso.PathfindPart.Position
						pathfindingModule.melee(humanoid, targetPosition)
					else
						humanoid:MoveTo(enemy.HumanoidRootPart.Position)
					end
				end)
			end
		end
	end
end)

I did read up on the documentation about the libraries, I just couldn’t think of any use cases much to be honest. I guess i’ll see when I have to use these when I’m scripting.

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