Function not looping through whole table

I’m trying to make bombs detonate when I press Q with a tool. But it’s only detonating one bomb at a time, any idea why?

function loopthrough(atable)
	for i,v in pairs(atable) do
		if v.Name == script.Parent.Parent.Name.."Bomb" then
			if v.Block:FindFirstChild("Exploded").Value == false then
				v.Block.bleep:Play()
				wait(0.6)
			local kaboom = Instance.new("Explosion")
			kaboom.BlastRadius = 10
			kaboom.Parent = v
			kaboom.Position = v.Block.Position
			kaboom.BlastPressure = 800000
			kaboom.Visible = false
			v.Block:FindFirstChild("Exploded").Value = true
			local what = game.ServerStorage["detonatepart"]:Clone()
			what.Position = v.Block.Position
			what.Parent = workspace
			what.ParticleEmitter:Emit(300)
			what.explode:Play()
			decimate(what,v:GetChildren(),v)-- wait 2 seconds, destroy
			bombnum.Value = bombnum.Value - 1
			debou = false
end
		end
	end
end
script.Parent.detonate.OnServerEvent:Connect(function()
loopthrough(workspace.Bombs:GetChildren())
	end)

This is why, it’s waiting for one bomb at a time. Using task.spawn or coroutines should work.

function loopthrough(atable)
	for i,v in pairs(atable) do
		if v.Name == script.Parent.Parent.Name.."Bomb" then
			task.spawn(function()
				if v.Block:FindFirstChild("Exploded").Value == false then
					v.Block.bleep:Play()
					wait(0.6)
					local kaboom = Instance.new("Explosion")
					kaboom.BlastRadius = 10
					kaboom.Parent = v
					kaboom.Position = v.Block.Position
					kaboom.BlastPressure = 800000
					kaboom.Visible = false
					v.Block:FindFirstChild("Exploded").Value = true
					local what = game.ServerStorage["detonatepart"]:Clone()
					what.Position = v.Block.Position
					what.Parent = workspace
					what.ParticleEmitter:Emit(300)
					what.explode:Play()
					decimate(what,v:GetChildren(),v)-- wait 2 seconds, destroy
					bombnum.Value = bombnum.Value - 1
					debou = false
				end				
			end)
		end
	end
end
script.Parent.detonate.OnServerEvent:Connect(function()
	loopthrough(workspace.Bombs:GetChildren())
end)

i’ve never understood coroutines, how do they work?

Scripts normally go line by line,

Thread 1: loop 1 → wait → explosion–> loop 2 – >wait → explosion–> …so on

coroutines bypass this

Thread 1: loop 1 → wait → explosion
Thread 2: loop 2 – >wait → explosion
Thread 3: … so on

This is my simplified explanation, try searching online for others.

1 Like

i’ve tried working with coroutines but i cant get this to work. any idea why?

local coroutinebomb = coroutine.create(function(object)
	if object.Name == script.Parent.Parent.Name.."Bomb" then
		if object.Block:FindFirstChild("Exploded").Value == false then
			object.Block.bleep:Play()
			wait(0.6)
			local kaboom = Instance.new("Explosion")
			kaboom.BlastRadius = 10
			kaboom.Parent = object
			kaboom.Position = object.Block.Position
			kaboom.BlastPressure = 800000
			kaboom.Visible = false
			object.Block:FindFirstChild("Exploded").Value = true
			local what = game.ServerStorage["detonatepart"]:Clone()
			what.Position = object.Block.Position
			what.Parent = workspace
			what.ParticleEmitter:Emit(300)
			what.explode:Play()
			decimate(what,object:GetChildren(),object)-- wait 2 seconds, destroy
			bombnum.Value = bombnum.Value - 1
			debou = false
end
end
end)
script.Parent.detonate.OnServerEvent:Connect(function()
	for i,v in pairs(game.Workspace.Bombs:GetChildren()) do
coroutinebomb.resume(v)
end
	end)

Coroutine.create only create one thread, you will need to loop through and create multiple threads so use .create multiple times.

Bruh guys you need to create a seperate thread for each item in the table, meaning that you have to create the new thread under the for i, v in pairs() line. In this case, I used task.defer(function()

function loopthrough(atable)
	for i,v in pairs(atable) do
		task.defer(function()
			if v.Name == script.Parent.Parent.Name.."Bomb" then
				if v.Block:FindFirstChild("Exploded").Value == false then
					v.Block.bleep:Play()
					wait(0.6)
					local kaboom = Instance.new("Explosion")
					kaboom.BlastRadius = 10
					kaboom.Parent = v
					kaboom.Position = v.Block.Position
					kaboom.BlastPressure = 800000
					kaboom.Visible = false
					v.Block:FindFirstChild("Exploded").Value = true
					local what = game.ServerStorage["detonatepart"]:Clone()
					what.Position = v.Block.Position
					what.Parent = workspace
					what.ParticleEmitter:Emit(300)
					what.explode:Play()
					decimate(what,v:GetChildren(),v)-- wait 2 seconds, destroy
					bombnum.Value = bombnum.Value - 1
					debou = false
				end
			end
		end)
	end
end
script.Parent.detonate.OnServerEvent:Connect(function()
	loopthrough(workspace.Bombs:GetChildren())
end)