Return a signal from a function in OOP

Hello! I’ve been trying to return a signal from a fuction like:

function class.changed()
    return --a signal that can be connect like: class.changed:Connect(function)
end

So my commitments has been getting a module like signals (by sleitnick) and then on the function using 1 argument as a callback, then connecting the signal to the callback, but i keep getting an error:

17:36:26.296 ServerScriptService.test:13: attempt to call a table value - Studio

This is the code that returns the error:

local timer = timer_module.new(1)

function on_update(number)
	print(number)
end

timer:updated(on_update)

Currently this is my module script:

local Timer = {}
Timer.__index = Timer
Timer.__type = "Timer"

local Signal = require(script.Parent.Signal)

function Timer.new(Time: number)
	local self = setmetatable({}, Timer)
	self.updated = Signal.new()
	self.timeout = Signal.new()
	self.time = Time
	self.active = false
	self.finish_time = 0
	self.time_elapsed = 0
	return self
end

function Timer:start()
	self.active = true
	
	while self.active and self.time_elapsed <= self.time do
		self.time_elapsed += .1
		self.updated:Fire(self.time_elapsed)
		task.wait(.1)
	end
	
	self.active = false
	self.timeout:Fire(self.time_elapsed)
end

function Timer:stop()
	if self.active then
		self.active = false
		self.timeout:Fire(self.time_elapsed)
	end
end

function Timer:updated(callback): number
	self.updated:Connect(callback)
end

function Timer:timeout(callback: () -> ())
	self.timeout:Connect(callback)
end

return Timer

Both the signal and method are named updated so they are conflicting. When you call timer:updated(on_update), you’re actually attempting to call a method on the signal object and not the method. You need to have distinct names for them. That applies for timeout as well

1 Like

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