How to use coroutines with a function inside a module script

If I’m using a function that’s inside a module script, where should I create the coroutine, in the script that calls the module script function, or inside the module script? This function should run with the original script, that’s why I need coroutine, but I don’t how to use it in this case

Should I do it like this:

skillsModule.damagePlr2 = coroutine.wrap(function()

-- Code

end)

Inside the module?

Make sure to add coroutine.resume(skillsModule.damagePlr2) and to make sure that skillsModule.damagePlr2 is already defined.

to call the function I do coroutine.resume(skillsModule.damagePlr2(...))?

what if I use wrap instead? can I just say skillsModule.damagePlr2(...)

Using warp would also work too.
If you’re not returning any values then you shouldn’t define skillsModule.damagePlr2 otherwise just make sure skillsModule.damagePlr2 exists.

if you need a new coroutine to be created more than once, depending on your use case i think it makes most sense to have the function standalone in the module and then use coroutine.create in the “original script”. in your above example coroutine.wrap will create a new thread once, when you require the module script, but everytime that thread is called afterward it will end up resuming the coroutine instead.

local dmgPlr2 = coroutine.create(skillsModule.DamagePlr2(...))

coroutine.resume(dmpPlr2)

Like this?

it’s saying that it can’t resume a dead coroutine when I use coroutine.wrap, should I add coroutine.yield at the end of the script? or something like that?

Can I see the code?
Make sure there are no errors in the code.

1 Like
skillsModule.damagePlr2 = coroutine.wrap(function(position, size, dmg, x, duration)

	local pos1 = position - (size / 2)
	local pos2 = position + (size / 2)
	
	local region = Region3.new(pos1, pos2)
	
	local partsInRegion = game.Workspace:FindPartsInRegion3(region)
	
	local index = 0
	
	while wait(x) do
		
		index = index + 1
		
		for i, v in pairs(partsInRegion) do
		
			if v.Parent and v.Parent:FindFirstChild("Humanoid") ~= nil and v.Name == "HumanoidRootPart" then
				
				v.Parent:FindFirstChild("Humanoid"):TakeDamage(dmg)
		
			end
	
		end
		
		if index == duration then
			
			break
			
		end
		
	end
	
	--coroutine.yield()

end)
skillsModule.damagePlr2(hitbox.Position, hitbox.Size, 10, 0.5, 8)

The code works, but something is wrong with how I used the coroutine.wrap

1 Like

You’re not returning a value try removing skillsModule.damagePlr2.
Just do a warp call.

1 Like

wdym? at the end skillsModule is returned

1 Like

Oh I see, don’t do a warp then.
Do create and call a resume later.

2 Likes

This could be the reason you are getting that error, assuming you are calling the function twice:


Yep!

3 Likes

Okay let me try it (30 characters)

1 Like

it works but when the function ends I get this error

00:52:26.306 - ServerScriptService.Skills (Server):98: missing argument #1 to 'create' (function expected)

1 Like

Can you give us your code?
You might have forgot function().

1 Like
local dmgPlr2 = coroutine.create(skillsModule.damagePlr2(hitbox.Position, hitbox.Size, 10, 0.5, 8))
coroutine.resume(dmgPlr2)
skillsModule.damagePlr2 = function(position, size, dmg, x, duration)
 -- Code
end
1 Like

Overlooked it!, you are calling the function, you need to pass in just the function, and then include the needed arguments in the coroutine.resume function

local dmgPlr2 = coroutine.create(skillsModule.damagePlr2)
coroutine.resume(dmgPlr2,hitbox.Position, hitbox.Size, 10, 0.5, 8)
1 Like

Do

local dmgPlr2 = coroutine.create(function() skillsModule.damagePlr2(hitbox.Position, hitbox.Size, 10, 0.5, 8) end)
coroutine.resume(dmgPlr2)
3 Likes