Define self in function table?

Is there any way to define self in a table function so I don’t have to declare an empty variable to put the function in early? In my case:

local ragDollTimer_rount
ragDollTimer_rount = globalfunc.coroutine.create(function()
	while true do
		if ragDollTimer.Value > 0 then
			ragDollTimer.Value = ragDollTimer.Value - 10
			wait(.1)
		else
			if canRecover.Value then
				ragDollValue.Value = false
			end
			ragDollTimer_rount:yield()
		end
	end
end)

Goal

local ragDollTimer_rount = globalfunc.coroutine.create(function()
	while true do
		if ragDollTimer.Value > 0 then
			ragDollTimer.Value = ragDollTimer.Value - 10
			wait(.1)
		else
			if canRecover.Value then
				ragDollValue.Value = false
			end
			self:yield()
		end
	end
end)

Looks like you want coroutine.running(). That returns the thread that it is called within.

2 Likes

In my case, I need to check if the coroutine is running or not, and coroutine.running() just returns the thread from which it runs on. Coroutine.status will not work, since it always returns suspended in my case. Here I just have a module function that creates a table that contains the coroutine and a boolean that determines if it runs for not. Yielding within that module makes the bool false, and resuming it within the module makes the bool true.

mod.coroutine = {

create = function(stuff,...)
	local self = {}
	setmetatable(self, mod.coroutine)
	
	self.Running = false
	self.Coroutine = coroutine.create(stuff, ...)
	return self
end,

resume = function(self,...)
	coroutine.resume(self.Coroutine,...)
	self.Running = true
end,

yield = function(self,...)
	self.Running = false
	coroutine.yield(self.Coroutine, ...)
end

}
mod.coroutine.__index = mod.coroutine