How can i use coroutine.wrap with a module function

Hi. How can i use coroutine.wrap with a module function. I have a module function and i wanted to use coroutine.wrap so that i don’t have to wait until the end of the function. i tried everything but i couldn’t find the answer. :thinking:

1 Like

You just have to pass the function to coroutine.wrap?

1 Like

i tried but it didin’t work it said missing argument #1

1 Like

Are you doing something like this?

local myModule = require(MyModuleScipt)

local coro = coroutine.wrap(myModule.MyFunction)
1 Like

i did it like this.

			-- zombie
			local zombie = enemyModule.spawn(zombieName);
			local zombieWrap = coroutine.wrap(zombie:move());
			zombieWrap();
1 Like

in the module, you can return function rather than a table in task.spawn, or put your yielding part into task.spawn

1 Like

move() with parentheses calls the function, so you are actually passing the result of :move() to wrap. If move returns nil then that would give you the error you see. You should pass the function itself:

	local zombie = enemyModule.spawn(zombieName);
	local zombieWrap = coroutine.wrap(zombie.move);
	zombieWrap();

Edit: Dot . not Colon :

it didin’t work i got a error it said expected (, {

1 Like

Hmmm. It may not work anyway because move is a member function and wants to be called with :. You may have to do something less obvious, I will check.

i am using Egomoose’s constructor.

example:

local Vector3 = {}
Vector3.__index = Vector3

-- constructor

function Vector3.new(x, y, z)
	local self = setmetatable({}, Vector3)
	
	self.x = x or 0
	self.y = y or 0
	self.z = z or 0
	
	return self
end

Why are you using this? This could cause some confusing issues because the returned self is a Table instead of a Userdata.

it’s easier for my enemy spawn script:

local serverStorage = game:GetService("ServerStorage");
local enemies = serverStorage:WaitForChild("Enemies");

local map = game.Workspace:WaitForChild("Map");
local waypoints = map:WaitForChild("Waypoints");

local enemy = {}
enemy.__index = enemy

function enemy.spawn(enemyName)
	local self = setmetatable({}, enemy)
	
	-- enemy exist
	local enemyExist = enemies:FindFirstChild(enemyName);
	if enemyExist then
		
		-- new enemy
		self.newEnemy = enemyExist:Clone();
		self.newEnemy.HumanoidRootPart.CFrame = waypoints:FindFirstChild("1").CFrame;
		self.newEnemy.Parent = game.Workspace;
	end
	
	return self
end

function enemy:move()
	
	-- waypoints
	for i = 1, #waypoints:GetChildren() do
		if self.newEnemy:FindFirstChild("Humanoid") then
			self.newEnemy.Humanoid:MoveTo(waypoints[i].Position);
			self.newEnemy.Humanoid.MoveToFinished:Wait();
		end
	end
	
	-- destroy
	self.newEnemy:Destroy();
end

return enemy

isn’t wrap and then resume same thing as task.spawn?

task.spawn(function()
    zombie:move()
end)

Why not this one

Also what I meant was;

local Vector3 = {}
Vector3.__index = Vector3

-- constructor

function Vector3.new(x, y, z)
	local self = setmetatable({}, Vector3)
	
	self.x = x or 0
	self.y = y or 0
	self.z = z or 0
	
	return self
end

return function(desiredFunction,...)
    if Vector3[desiredFunction] then
        task.spawn(function()
            Vector3[desiredFunction](...)
        end
    end
end --something like this

i’ll try it and see if it works.

If you want to pass in a table function that uses the colon operator to a task.spawn or coroutine.wrap, you must switch to dot operator and pass the table as the second argument.

task.spawn(zombie.move, zombie, ...)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.