Brand New Coroutine "Cannot resume dead coroutine"

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Using a coroutine to quickly generate a world

  2. What is the issue? Include screenshots / videos if possible!
    When using coroutine.resume(), it says
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using coroutine.wrap(), but for some reason it just crashes my Studio.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local partContainer = workspace:WaitForChild("World")

local fogScreenSize = 25
local resolution = 100
local frequency = 3
local amplitude = 10
local seed = os.time()
local startPos = Vector3.new(-37,67,-37)
local routines = {}
local statuses = {}

local function getHeight(x,z)
	local noiseHeight = math.noise(x / resolution*frequency,z / resolution*frequency,seed)
	
	return noiseHeight
end

local chunks = {}

local function compressWorldToChunk(parts, chunkX, chunkZ)
	local partsCopy = parts
	local partNum = math.random(1, #partsCopy)
	local part = partsCopy[partNum]
	table.remove(partsCopy, partNum)
	local union = part:UnionAsync(partsCopy)
	union.Parent = game.Workspace.Chunks
	chunks[tostring(chunkX)..","..tostring(chunkZ)] = union
	for i, v in pairs(game.Workspace.World:GetChildren()) do
		v:Destroy()
	end
	return union
end

local function makeChunk(chunkx, chunkz)
	local parts = {}
	for x = 0, fogScreenSize do
		for z = 0, fogScreenSize do
			local part = Instance.new("Part")
			part.Parent = partContainer
			part.Anchored = true
			part.Size = Vector3.new(1,1,1)
			part.BrickColor = BrickColor.new("Parsley green")
			part.Material = Enum.Material.SmoothPlastic
			
			local height = getHeight(x+(chunkx*fogScreenSize),z+(chunkz*fogScreenSize))

			part.Position = Vector3.new(x, height*amplitude, z) + Vector3.new(-37+(chunkx*fogScreenSize),67,-37+(chunkz*fogScreenSize))
			
			table.insert(parts, part)	
		end

		game:GetService("RunService").Heartbeat:Wait()
	end
	compressWorldToChunk(parts, chunkx, chunkz)

	return parts
end

for i = 0, 8 do
	local func = coroutine.create(function()
		for v = 0, 8 do
			local parts = makeChunk(i, v)
		end
	end)
	coroutine.resume(func)
	table.insert(routines, func)
end

local all = true
repeat
	for i, v in pairs(routines) do
		if coroutine.status(v) == "dead" then
			all = false
		end
	end
until all

for i, v in pairs(routines) do
	coroutine.close(v)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

hi, I am a little confused, but I’m pretty sure you can just replace

for i = 0, 8 do
	local func = coroutine.create(function()
		for v = 0, 8 do
			local parts = makeChunk(i, v)
		end
	end)
	coroutine.resume(func)
	table.insert(routines, func)
end

local all = true
repeat
	for i, v in pairs(routines) do
		if coroutine.status(v) == "dead" then
			all = false
		end
	end
until all

for i, v in pairs(routines) do
	coroutine.close(v)
end

with

for i = 0, 8 do
	for v = 0, 8 do
		coroutine.wrap(makeChunk)(i, v)
	end
end

since coroutines do not return values. Is using a coroutine even necessary since there are no wait conditions?

I was using a function to make it into a union for a performance boost
Edit: OHHHH I see now
but like that would probably crash my game because it would create over 60 coroutines

ah in that case this can be done by

local partContainer = workspace:WaitForChild("World")

local fogScreenSize = 25
local resolution = 100
local frequency = 3
local amplitude = 10
local seed = os.time()
local startPos = Vector3.new(-37,67,-37)
local routines = {}
local statuses = {}

local function getHeight(x,z)
	local noiseHeight = math.noise(x / resolution*frequency,z / resolution*frequency,seed)
	
	return noiseHeight
end

local chunks = {}

local function compressWorldToChunk(parts)
	local partsCopy = parts
	local partNum = math.random(1, #partsCopy)
	local part = table.remove(partsCopy, partNum)
	local union = part:UnionAsync(partsCopy)
	union.Parent = game.Workspace.Chunks
	chunks[tostring(chunkX)..","..tostring(chunkZ)] = union
	for i, v in pairs(game.Workspace.World:GetChildren()) do
		v:Destroy()
	end
	return union
end

local function makeChunk(chunkx, chunkz)
	local parts = {}
	for x = 0, fogScreenSize do
		for z = 0, fogScreenSize do
			local part = Instance.new("Part")
			part.Parent = partContainer
			part.Anchored = true
			part.Size = Vector3.new(1,1,1)
			part.BrickColor = BrickColor.new("Parsley green")
			part.Material = Enum.Material.SmoothPlastic
			
			local height = getHeight(x+(chunkx*fogScreenSize),z+(chunkz*fogScreenSize))

			part.Position = Vector3.new(x, height*amplitude, z) + Vector3.new(-37+(chunkx*fogScreenSize),67,-37+(chunkz*fogScreenSize))
			
			table.insert(parts, part)	
		end

		game:GetService("RunService").Heartbeat:Wait()
	end

	return parts
end

local unions = {}
for i = 0, 8 do
	local parts = {}
	for v = 0, 8 do
		table.insert(parts, makeChunk(i, v))
	end
	table.insert(unions, compressWorldToChunk(parts))
end

assuming that the rest of the script works properly

1 Like

table.remove returns the value of the removed item/entry.

local part = partsCopy[partNum]
table.remove(partsCopy, partNum)
local part = table.remove(partsCopy, partNum)
1 Like

ohhh i didn’t know that when i wrote the original script