Coroutine.wrap and task.spawn not working on module functions

So I have made a tower system, and I am planning on making a while loop for every tower spawned in the game, I will give a small snippet at the part where its not working.

The thing is the latest tower only gets to attack while the rest are sitting ducks. (this module script is required by both server and client for client sided rendering effect)

Attack Module:

local normalAttack = require(script:WaitForChild("Normal"))
local splashAttack = require(script:WaitForChild("Splash"))

local towers = require(rs:WaitForChild("RealTimeModules"):WaitForChild("Towers"))

function attack.attack(tower, RealTower)
	tower = towers[tower]
	if tower.Type == "N" then
		coroutine.wrap(normalAttack.Attack)(tower, RealTower)
	elseif tower.Type == "S" then
		coroutine.wrap(splashAttack.Attack)(tower, RealTower)
	end
end
1 Like

Are the functions that are resumed asynchronously defined with colon notation? If yes, you’ll also need to pass the module itself as a parameter.

Can you elaborate? I didn’t understand

In your module, did you do:

function module.method()
end

or

function module:method()
end

Because colon notation passes the module itself as a parameter, and since you’re spawning with dot notation (you can’t spawn with colon notation), you’d need to explicitely pass the module as a parameter too.

module:method()
--is the same as
module.method(module)
1 Like

I have done Normal.attack not Normal:Attack

One interesting thing I have noticed after adding prints in the normal.attack is the server prints how much towers are there while client only prints the latest tower only.

So it’s a synchronization issue? Requiring a module in both the server and client doesn’t magically share modules, they both run in their own cached version in their respective environment. coroutines/threads work just fine in modules.

1 Like