Getting Error When Trying To Connect Function To Coroutine.Wrap

I’m trying to use the function coroutine.wrap() so that I can make it so that my function doesn’t yield, as I fire the function 5 times to spawn a copy of a bomb that goes through a long process, and it yields.

I saw another post that told me that coroutine.wrap() doesn’t yield the function that its connected to, but after inserting it into my code, for some reason I get this error.

  18:41:59.584  Workspace.Cluster Bomb.PlantBomb:224: missing argument #1 to 'wrap' (function expected)  -  Server - PlantBomb:224
  18:41:59.584  Stack Begin  -  Studio
  18:41:59.585  Script 'Workspace.Cluster Bomb.PlantBomb', Line 224 - function explode  -  Studio - PlantBomb:224
  18:41:59.585  Script 'Workspace.Cluster Bomb.PlantBomb', Line 240  -  Studio - PlantBomb:240
  18:41:59.585  Stack End  -  Studio

Here’s the code.

function miniBombExplode(miniBomb, user)
	for i = 1, 3 do task.wait(.75)
		tickSound:Play()
	end

	task.wait(1)

	miniBomb.Anchored = true

	local explosionParticle = miniBomb.Explosion

	explosionSound:Play()

	miniBomb.Transparency = 1
	miniBomb.Decal.Transparency = 1

	local region3 = Region3.new(
		miniBomb.Position - Vector3.new(blastRegion3L/2, 1, blastRegion3W/2),
		miniBomb.Position + Vector3.new(blastRegion3L/2, blastRegion3H/2, blastRegion3W/2)
	) -- Creates a region3 that destroys any parts in the explosions radius.

	local region3Visual = Instance.new("Part") -- The visualized part for the region3.
	region3Visual.Name = "blastRadiousVisual"
	region3Visual.CanCollide = false
	region3Visual.Size = Vector3.new(blastRegion3L, blastRegion3H, blastRegion3W)
	region3Visual.CFrame = region3.CFrame
	region3Visual.Anchored = true
	region3Visual.Transparency = .5
	region3Visual.Parent = miniBomb
	region3Visual.BrickColor = BrickColor.new("Persimmon")

	local explosion = Instance.new("Explosion", miniBomb) -- The explosion instance
	explosion.Position = miniBomb.Position
	explosion.BlastRadius = 0
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastPressure = 1000000

	explosion.BlastRadius = blastRegion3L

	local overlapParams = OverlapParams.new()
	overlapParams.FilterDescendantsInstances = {tool, tool.Handle, region3Visual}

	local parts = workspace:GetPartBoundsInBox(region3.CFrame, region3.Size, overlapParams)

	explosion.Hit:Connect(function(Part)
		if Part.Name == "HumanoidRootPart" then
			local character = Part.Parent
			local player = game.Players:GetPlayerFromCharacter(character)

			if player and player.Team ~= user.Team then
				character:FindFirstChildOfClass("Humanoid"):TakeDamage(baseDamage)

				if character:FindFirstChildOfClass("Humanoid").Health == 0 then
					user.leaderstats.Kills.Value += 1
				end
			end
		end
	end)

	for i, v in pairs(parts) do
		if v.Name == "Brick" then
			v.Anchored = false

			v.CanTouch = false

			v.BrickColor = user.TeamColor

			for i, constraint in pairs(v:GetChildren()) do
				if constraint:IsA("Snap") or constraint:IsA("Weld") or constraint:IsA("WeldConstraint") then
					constraint:Destroy()
				end
			end

			task.delay(3, game.Destroy, v)
		end

		if v.Name == "GameBrick" then v.Name = "TaggedGameBrick" -- TaggedGameBricks are GameBricks in the game that have been effected by the bomb, while regular Bricks are just bricks you can destroy by default.
			v.Anchored = false

			v.CanTouch = false

			user.leaderstats.Bricks.Value += 1 -- The total bricks the player has broken throughout the round.
			user["T. Bricks"].Value += 1 -- The total bricks that the player has destroyed throughout there playtime
			user.leaderstats.Studs.Value += .1 -- Increases the players currency (Studs) by 0.1.
			v.BrickColor = user.TeamColor -- Changes the taggedBricks color to the local players team color to show that they have broken the part.

			task.delay(3, game.Destroy, v) -- later deletes the part.
		end
	end

	task.delay(3, game.Destroy, miniBomb)

	task.wait(2)

	explosionParticle.Enabled = false -- Disables the explosion particles.
end
....

	for i = 0, 5, 1 do
		local miniBombClone = Mini_Bomb:Clone()
		miniBombClone.Parent = workspace.Bombs
		miniBombClone.Position = bomb.Position + Vector3.new(math.random(-4, 4), 4, math.random(-4, 4))
		
		coroutine.wrap(miniBombExplode(miniBombClone, user))
	end

coroutine.wrap(miniBombExplode(miniBombClone, user))()

the extra () at the end should fix it

That’s weird, it didn’t seem to do much…

I’m still getting the same error, and only one is spawning when I want them all to spawn at the same time.

coroutine.wrap(function()
miniBombExplode(miniBombClone, user)
end)()

this should fix it, i hope

1 Like

It did end up fixing the issue.

May I ask how two parentheses changed the entire code?

I actually don’t know. But from what I’ve experienced, it lets the function inside of it run.

1 Like

you could also use task.spawn, where there’s no need for those extra ()

Example:

task.spawn(function()
	-- code here
end)

for some reason I get this error.
missing argument #1 to 'wrap' (function expected)

it says it needs a function

-- you're calling the 'miniBombExplode' function, not passing it
coroutine.wrap(miniBombExplode(miniBombClone, user))()
-- this would work but you want to pass 'miniBombClone' & 'user'
coroutine.wrap(miniBombExplode)()
coroutine.wrap(miniBombExplode)(miniBombClone, user)

but you should try using task.spawn like @wGummi said

What’s the difference from coroutine.wrap and task.spawn()?

i know you can yield and resume with coroutine
other than that, i honestly don’t know the difference between spawn and coroutine

just wanted to note that you can pass arguments with task.spawn incase you do use it

task.spawn(miniBombExplode, miniBombClone, user)
1 Like

I don’t know the exact difference except for the extra coroutine methods as @BonesIsUseless said above. Other than that, task.spawn is newer and probably considered better practice.

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