How to use coroutines with modules?

How can i use coroutine (which i’ve never used before, so bear with me) with module scripts, i have a function inside the module script, and it all works fine, but i want the script that calls that module script to continue, even when the module script is doing task.waits and things like that. How could i achieve this?

Server Script:

abilityModule.Q(plr, plr.Character)

Module Script:

local module = {}

local speedUpSound = game.ServerStorage.SFX.Speed

function module.F(plr, char)
	local speedSound = speedUpSound:Clone()
	speedSound.Parent = workspace
	speedSound:Play()
	local humanoid = char:WaitForChild("Humanoid")
	humanoid.WalkSpeed = 25
	
	task.wait(10)
	humanoid.WalkSpeed = 16
end

return module

Thanks in advance!

coroutine.wrap(abilityModule.Q)(plr, plr.Character)

So, in the coroutine.wrap function, the firsy argument is a function, which you want to run it without yielding, the coroutine.wrap then returns a function that can be called by calling it (), you can also pass your desired arguments while calling the returned function, well… i usually use coroutine.wrap if i find a necessary situation, so i really don’t have experience with other functions of the coroutine table so sorry.

Thanks, i did:

local ability = coroutine.wrap(abilityModule.F(plr, plr.Character))
				
ability()

but i sadly got this error, which says it is’nt a function, which it is?

“missing argument #1 to ‘wrap’ (function expected”

That is not how you should do it. As the argument of the coroutine.wrap function you have to pass the reference to the function
Example:

local Func=function(arg1)
print(arg1)
end
local newFunction=coroutine.wrap(Func)
newFunction'string'

coroutine can be really dangerous in modules, it’s safer to use task.spawn or task.defer which is basically the same thing but safer with require being require.

For the op, you use task spawn like this:

task.spawn(f, args)

-- so for example
local function printMsg(msg)
  print(msg)
end

task.spawn(printMsg, "Hello World!")

how would i do it in my server script, as im tryinng to get a module function. would it be something like

local abilityFunc = abilityModule.F()
local newAbilityFunc = coroutine.wrap(abilityFunc)

newAbilityFunc(plr, plr.Character)

That’s true, honestly i find task.defer superior to the task.spawn

Example

local yourModule=require(module)

local NewFunc=coroutine.wrap(yourModule.YourFunction)
NewFunc(examplearg)

so i could do

local function module.F()

task.spawn(module.F, plr, plr.Character)

Yes, task.spawn/defer takes the function itself, then every arg you put in after is passed to that function in a new thread.

Got it, didnt @metatablecatmaid say coroutine could be unreliable, do u think itll be ok?

Which should i use?

ehh, its very technical but i’ll summarise as saying, require creates a coroutine to run the module in, and if you coroutine.resume within that module, the threads require is yielding never get a response.

This doesn’t happen with task because task doesn’t care about responses.

I think using task.defer is a good choice, as i said before i do not really have enough experience with coroutine, so thanks to the @metatablecatmaid for sharing that point with us.

Oh, i understand now, which would you use, as with task it doesnt matter whats in the module, which is sort of what i want. I guess it doesnt matter which one i use, its just up to which one is easier to do.