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)
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)