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