Cloning functions?

Is it possible if I can clone functions? The function I want to use loops itself with different tables so it’s reasonable that I would use coroutine.

The problem is that when I call coroutine.yield inside the function. It stops every coroutine that’s running related to the function. I just want a specific coroutine to be stopped.

local TaskTable = {}

local numTable = {1,2,3}
local stringTable = {"One","Two","Three"}

local active = true

local function define(array)
	while true do
		for i,v in ipairs(array) do
			print(v)
			wait(1)
		end
		if not active then
			coroutine.yield() -- Return could also work.
		end
	end
end

local NewTask = coroutine.create(define)
coroutine.resume(NewTask,numTable)

wait(4)

local NewerTask = coroutine.create(define)
coroutine.resume(NewerTask,stringTable)

wait(3)
print("Pause!")
active = false

--[[Output
  17:34:53.240  1  -  Server - AA:12
  17:34:55.776  2  -  Server - AA:12
  17:34:58.107  3  -  Server - AA:12
  17:34:58.146  One  -  Server - AA:12
  17:34:59.120  1  -  Server - AA:12
  17:34:59.153  Two  -  Server - AA:12
  17:35:00.137  2  -  Server - AA:12
  17:35:00.154  Three  -  Server - AA:12
  17:35:01.138  3  -  Server - AA:12
  17:35:01.154  Pause!  -  Server - AA:30
]]

I just want the function to stop printing the numTable, not both of them.

I’m not EXACTLY sure what you mean, but if you mean you want to use the same function in multiple scripts, you would need to use a ModuleScript.

Something like this

local TaskTable = {}

local numTable = {1,2,3}
local stringTable = {"One","Two","Three"}

local function define(task, array)
	TaskTable[task] = true
	while true do
		for i,v in ipairs(array) do
			print(v)
			wait(1)
		end
		if TaskTable[task] == nil then
			coroutine.yield() -- Return could also work.
		end
	end
end

local NewTask = coroutine.create(define)
coroutine.resume(NewTask,NewTask,numTable)

wait(4)

local NewerTask = coroutine.create(define)
coroutine.resume(NewerTask,NewerTask,stringTable)

wait(3)
print("Pause!")
TaskTable[NewTask] = nil
--[[
  19:54:18.516  1  -  Server - Script:10
  19:54:19.927  2  -  Server - Script:10
  19:54:20.939  3  -  Server - Script:10
  19:54:21.954  1  -  Server - Script:10
  19:54:22.523  One  -  Server - Script:10
  19:54:22.971  2  -  Server - Script:10
  19:54:23.538  Two  -  Server - Script:10
  19:54:23.972  3  -  Server - Script:10
  19:54:24.555  Three  -  Server - Script:10
  19:54:24.988  1  -  Server - Script:10
  19:54:25.539  Pause!  -  Server - Script:28
  19:54:25.556  One  -  Server - Script:10
  19:54:25.989  2  -  Server - Script:10
  19:54:26.556  Two  -  Server - Script:10
  19:54:27.005  3  -  Server - Script:10
  19:54:27.564  Three  -  Server - Script:10
  19:54:28.571  One  -  Server - Script:10
  19:54:29.572  Two  -  Server - Script:10
  19:54:30.589  Three  -  Server - Script:10
  19:54:31.604  One  -  Server - Script:10
  19:54:32.621  Two  -  Server - Script:10
  19:54:33.639  Three  -  Server - Script:10
]]

OHHhh!! Nice! This was exactly what I was looking for! Thanks so much man, saved me so many hours. Sweet…

Edit: Didn’t think to add more arguments inside coroutine.resume() and prob never would

if a function is running with an argument and then you call that function again with a different argument it’s gonna override the current running one. Assuming it’s in ModuleScript.

I did it one time accidentally when I was tweening a gui, and that GUI never completely stopped because I kept changing the argument.