What would happen if I wrap every line in task.spawn?

Ok, so random question.

I have been debugging, and knowing that task.spawn() runs every line that you wrap it at the same time, I also had them error, and the rest of the script still works.

I know you can wrap every line in pcalls to prevent errors, but could you bypass all errors with task.spawn() like this:

local AllCategories = {}

task.spawn(function()
	for _, Categories in ipairs(game:GetService("ReplicatedStorage").models:GetChildren()) do
		task.spawn(function()
			table.insert(AllCategories, Categories:GetAttribute("Category"))
		end)
	end
end)

task.spawn(function()
	for _, Category in ipairs(AllCategories) do
		task.spawn(function()
			if not script.Parent:FindFirstChild(Category) then
				task.spawn(function()
					local CategoryButton = script.CategoryTemplate:Clone()
					task.spawn(function()
						CategoryButton.Text = Category
						task.spawn(function()
							CategoryButton.Name = Category
							task.spawn(function()
								CategoryButton.Visible = true
								task.spawn(function()
									CategoryButton.Parent = script.Parent
									task.spawn(function()
										CategoryButton.MouseButton1Click:Connect(function()
											task.spawn(function()
												for _, Models in ipairs(game:GetService("ReplicatedStorage").models:GetChildren()) do
													task.spawn(function()
														if Models:GetAttribute("Category") == Category then
															task.spawn(function()
																local ModelTemplate = script.ModelTemplate:Clone()
																task.spawn(function()
																	local Model = Models:Clone()
																	task.spawn(function()
																		Model.Parent = ModelTemplate.ViewportFrame.WorldModel
																		task.spawn(function()
																			local ModelCamera = Instance.new("Camera")
																			task.spawn(function()
																				ModelCamera.CFrame = CFrame.new(
																					(Model.PrimaryPart.CFrame * CFrame.new(10, 15, -20)).Position
																						+ Vector3.new(3, -2, 0), 
																					Model.PrimaryPart.Position
																				)
																				task.spawn(function()
																					ModelCamera.FieldOfView = 30
																					task.spawn(function()
																						ModelCamera.Parent = ModelTemplate.ViewportFrame
																						task.spawn(function()
																							ModelTemplate.ViewportFrame.CurrentCamera = ModelCamera
																							task.spawn(function()
																								ModelTemplate.TextLabel.Text = Model:GetAttribute("Alias")
																								task.spawn(function()
																									ModelTemplate.Parent = script.Parent.Parent.ScrollingFrame.Frame
																								end)
																							end)
																						end)
																					end)
																				end)
																			end)
																		end)
																	end)
																end)
															end)
														end
													end)
												end
											end)
										end)
									end)
								end)
							end)
						end)
					end)
				end)
			end
		end)
	end
end)

The code above fully works in my game.

I am wondering how much performance it would cost, for this script, and a script with 10000 lines of code, as well as doing this in a big game like Jailbreak and Adopt Me (Every single script).

Obviously I would never do this (except for maybe April fools or something), as it makes the code really hard to read.

task.spawn(function()
	print("Thanks in advance!")
end) 
3 Likes

CPU utilization will probably explode

7 Likes

If every single script in Jailbreak did this, how would the game lag on the world’s fastest computer?

Before we continue with this Ted-Talk I do believe this thread should be moved into #development-discussion

1 Like

Alright, I moved it. I am afraid of this category lol.

Ok and now I’m going to disappear and let other people carry on this forensic discussion

2 Likes


This

Im already aware that it would make the code really unreadable, but how much performance would it cost?

A lot, mainly on the CPU and CPU time lol

2 Likes

And if every single script did this inside of Jailbreak or some other big game, would the fastest computer be able to process Jailbreak?

Almost certainly. Jailbreak isn’t very script heavy. It could cause a lot of functionality problems though. Most parts of a program aren’t designed to work out of sync with other parts, and Roblox’s task scheduler will be overworked.

2 Likes

Is the task scheduler like a Server on Roblox, so it would affect a lot of games?

No? The task scheduler is ran on the server/client, it would overload your game maybe

7 Likes

Neither pcall nor task.spawn prevent errors. They don’t fix your code. Fix your buggy code instead of writing code that will be an extreme pain to maintain in the future.

You forgot to spawn this as well.

5 Likes

I meant bypassing them, because code stops when there is an error.

I didn’t spawn Local variables that are blank.

Why not coroutine resume/create?

coroutine.resume(coroutine.create(function()
    -- code
end))

For real though, don’t do this lol. You should avoid using coroutines if you don’t need them, since it runs a separate thread from the main one. Using coroutines sparingly is fine, but using like 20+ is a bit much and could potentially impact performance, since it adds yet another thing for the task scheduler to execute (not to mention if the code wrapped in the coroutine is expensive).

3 Likes

task.spawn() is used for multithreading over cores.using it once a while wont do much but i dont believe it will affect Performance much but its kinda useless as roblox already utilises Cores/Threads.

1 Like

It’s not for true multithreading it just suspends the current thread to the scheduler and runs another one right away.

2 Likes

what is the diferance between coroutine and task.spawn?

Errors are supposed to stop your code because it means something unexpected happened and the code cannot continue. If you “bypass” it like that you’ll just get more errors along the way.

Also wrapping every line of code in a task.spawn in a normal game would drastically reduce your fps, firstly because even “task” aside you’d be creating a new lua closure for every line and lua functions are slow, and secondly because task.spawn runs the function in a new thread, ex. has to enqueue it in task scheduler, so you’d be blowing up the queue which could also possibly cause a crash sooner or later.

I kinda experienced the fps drop part myself once. I was attempting to make a mini lua-in-lua vm and I accidentally made it create a new function for every opcode execution which made my fps drop from 60 to 4.

3 Likes