Best way to do over a million things at the same time?

Hello Robloxians, I have a question. What is the best way to do over 1 million things at the same time, I tried using coroutine but it just gave me a script timeout error so I’m just really stuck here. I would really appreciate it if you could help.

11 Likes

The solution is to not run a million tasks at the same time

11 Likes

Yeah. Like in what world do you need to run a million tasks at the same?

4 Likes

A million? Like an actual million?

Just tell us what you’re trying to do.

3 Likes

Am trying to loop through a lot of numbers and load them into my game. For example, let’s say I wanted to load a rat, I would loop 1m numbers and insert them and check their name, if their name is Rat I clone them.

3 Likes

you mean for i = 1, 1000000000 do?

2 Likes

Shouldn’t you just clone what you want directly?

2 Likes

Use separate scripts or task.spawn(), or as @OnceCrowned said, don’t run that many tasks at the same time

2 Likes

Do you mean a random name or chance of spawning? You could use math.random(1,x) or if you mean loading 1 million objects at the exact same time then just don’t do that

2 Likes

? Just do a loop like this:

for i, rat: Model? in pairs(workspace:GetDescendants()) do
   if rat.Name == "Rat" then
      local clone = rat:Clone()
      clone.Parent = workspace
   end
end
2 Likes

The best way is to not do a million things at the same time in the first place.

3 Likes

No, I mean that I loop through all of The roblox account’s assets and see which one has the name of “Rat”.

There is only one model on the Roblox Account named “Rat” and its this one.

There is no reason to go through all of its assets for this model.
If you’re confused on how to insert it then use InsertService:LoadAsset()

1 Like

I have same problem as you, i dont know any better solutions than this:

local Table = workspace.Folder:GetChildren()
-- anyFolder:GetChildren() = table, and table = anyFolder:GetChildren()

for i, object in pairs(Table) do 
 task.spawn(function()
  -- Do your thing
 end)
end

I think any code like that will lag if you will ran it without wait() but wait waits 0.01,
so if your table will have 500 objects, wait will take your time to end the loop from 0 seconds to 5.

Hey, I hope I can help out with this, but I need to know more about what you are attempting to do?

Please refer to the xy problem info page.

Actual software engineer here. I haven’t been able to make sense of what you are trying to accomplish. You said that you are trying to loop through a bunch of numbers and load them into the game. Are these asset Id numbers? That’s a significant number of assets. The problem is that the machine cannot do 1,000,000 things at the same time. So that’s a no-go. However, you can break it up into smaller chunks.

local totalSize = 1000000
local blockSize = 10000
local blockCount = math.floor(totalSize / blockSize)
local nextBlock = 0
local mutex = false
local threadCount

-- Acquires the lock for exclusive access.
local function lock()
	while mutex == true do
		task.wait()
	end
	mutex = true
end

-- Releases the lock.
local function unlock()
	mutex = false
end

-- Performs the required task, calculating
-- the start and end block positions based
-- on the next block of items to process.
function doTask()
	while nextBlock < blockCount do
		-- Calculate start and end positions.
		count = blockSize - 1
		lock()
		start = nextBlock * blockSize
		nextBlock += 1
		unlock()

		-- Perform Task
		for i = start, count, 1 do
			-- Perform your task here.
		end

		-- Breaks the execution up to prevent
		-- script timeout errors.
		task.wait()
	end
end


for i = 1, threadCount, 1 do
	task.spawn(function()
		doTask()

		-- Introduces a slight delay to help prevent a race
		-- condition and to reduce lock contention.
		task.wait()
	end)
end

I just came up with this off the cuff, so I’m sure that there’s a bug or two. But that’s the jist of what you need to do. I haven’t tested this.

2 Likes

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