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
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.
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!")
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.