I need help with OOP

What should I do to be able to listen to access and modify object properties for strict checks while improving my class.
I want my class to be able to keep internal variables private from the outside

local SignalConnection = {}
SignalConnection.__index = SignalConnection
SignalConnection.__newindex = function(t, k ,v)
	if debug.info(2, "s") ~= traceback_id then
		error(string.format("Attempt to set SignalConnection.%s in a read-only table",tostring(k)), 2)
	end
	rawset(t, k ,v)
end
SignalConnection.__tostring = function(...)
	return "SignalConnection"
end
function SignalConnection.new(func, block)

	local self = setmetatable({
	}, {
		block = block,
		func = func,
		index = -1,
		isActive = true,
		__index = function(t, k)
			if debug.info(2, "s") == traceback_id then
				return getmetatable(t)[k]
			end
			if k == "Disconnect" then
				return SignalConnection.Disconnect
			elseif k == "isActive" then
				return getmetatable(t).isActive 
			end
		end,
		__newindex = function(t, k, v)
			if debug.info(2, "s") ~= traceback_id then
				error(string.format("Attempt to set SignalConnection.%s in a read-only table",tostring(k)), 2)
			end
			rawset(getmetatable(t), k ,v)
		end,
	})
	return self
end

function SignalConnection:Disconnect()
	if not self.isActive then
		warn("Connection has been not avtive. Cannot disconnect a Passive connection")
		return
	end
	local block = self.block
	local index = self.index
	local connections = block.connections
	connections[index] = nil
	block.counter -= 1
	self.removedTargetIndex = #block.removedIndices + 1
	block.removedIndices[self.removedTargetIndex] = index
	self.isActive = false
end

Looking at that code for a split second, the “read-only table” errors jumped out at me. While something like that would be necessary in regular Lua, Roblox’s Luau has table.freeze to (permanently as far I as know) make a Table read-only.

It also looks like you’re trying to create a generic framework for constructing your OOP objects as Tables dynamically, which I fully advise against – just make the Table types you actually use, as you need them!


To answer the question you actually asked, however, you should look into the Observer Pattern. I’d generally recommend against having “private” state by default, as simple Getter and Setter functions are a complete waste of time and energy. However, in this case, as you want to “listen” or “subscribe” to property changes, using functions to access propertiers that notify all Observers is an appropriate solution.

Alternatively, you can also use a proxy Table and dispatch Observer notifications via the __index and __newindex metamethods (similar to what you’re doing already in that code snippet), although I believe this is incompatible with table.freeze.

Thank for your answer. I’ll refer to your answer to improve. I did this because I am making a signal module and I use a block linked list to compensate for the advantages of arrays and linked lists. I don’t really want to have an external role using this module to access private variables . table.freeze is not very suitable for this, after all, properties need to be changed dynamically

it’s the full code of my module
(edit: I’ve rolled back my changes)

--[[

	[Signal] A Luau-Based Signal Implementation
	<https://devforum.roblox.com/u/lkkyuy>
	
	Adopt a block linked list:
	
	head -> [BLOCK] -> [BLOCK] <- tail
			/              \
		[C][C][C][C]  [C][C][C][C]	
	
	@Author : Lkkyuy (LookHappy)	
	@Date   : 8/12/2025	
	@Version: 0.0.1
]]
export type SignalConnection = {
	read isActive: boolean,
	Disconnect: (self: any) -> ()
}
export type Signal<T...> = {
	Wait: (self: any) -> T...,
	Connect: (self: any, func: (T...) -> ()) -> SignalConnection,
	Once: (self: any, func: (T...) -> ()) -> SignalConnection,
	GetConnectedCounter: (self: any) -> number
}
export type SignalModule = {
	new: <T...> (self: any, block_size: number?) -> Signal<T...>,
	Fire: <T...>(self: any, T...) -> ()
}
local function HandleThreadAndCallEvent(block, func, ...)
	local thread = block.freeConnectionThread
	block.freeConnectionThread = nil
	func(...)
	block.freeConnectionThread = thread
end

local function ConnectedSignalHandler(block)
	while true do
		HandleThreadAndCallEvent(block, coroutine.yield())
	end
end

local traceback_id = debug.info(1, "s")
local Signal = {}
Signal.__tostring = function(...)
	return "Signal"
end
Signal.__index = Signal
Signal.__newindex = function(t, k ,v)

	if debug.info(2, "s") ~= traceback_id then
		error(string.format("Attempt to set Signal.%s in a read-only table",tostring(k)), 2)
	end
	rawset(t, k ,v)
end

local SignalConnection = {}
SignalConnection.__index = SignalConnection
SignalConnection.__newindex = function(t, k ,v)
	if debug.info(2, "s") ~= traceback_id then
		error(string.format("Attempt to set SignalConnection.%s in a read-only table",tostring(k)), 2)
	end
	rawset(t, k ,v)
end
SignalConnection.__tostring = function(...)
	return "SignalConnection"
end
function SignalConnection.new(func, block)
	--local self = setmetatable({}, {
	--	block = block,
	--	func = func,
	--	index = -1,
	--	isActive = true,
	--	__index = function(t, k)
	--		if debug.info(2, "s") == traceback_id then
	--			return getmetatable(t)[k]
	--		end
	--		if k == "Disconnect" then
	--			return SignalConnection.Disconnect
	--		elseif k == "isActive" then
	--			return getmetatable(t).isActive 
	--		end
	--	end,
	--	__newindex = function(t, k, v)
	--		if debug.info(2, "s") ~= traceback_id then
	--			error(string.format("Attempt to set SignalConnection.%s in a read-only table",tostring(k)), 2)
	--		end
	--		rawset(getmetatable(t), k ,v)
	--	end,
	--})
	local self = {
		
		block = block,
		func = func,
		index = -1,
		isActive = true,
		
	}
	return self
end

function SignalConnection:Disconnect()
	if not self.isActive then
		warn("Connection has been not avtive. Cannot disconnect a Passive connection")
		return
	end
	local block = self.block
	local index = self.index
	local connections = block.connections
	connections[index] = nil
	block.counter -= 1
	self.removedTargetIndex = #block.removedIndices + 1
	block.removedIndices[self.removedTargetIndex] = index
	self.isActive = false
end



local SignalConnectionBlock = {}
SignalConnectionBlock.__index = SignalConnectionBlock

function SignalConnectionBlock.new(size: number, once: boolean?)
	local self = {
		_next = nil,
		_prev = nil,
		freeConnectionThread = nil,
		removedIndices = table.create(size),
		connections = table.create(size),
		once = once or false,	
		size = size,
		counter = 0,
	}
	return setmetatable(self, SignalConnectionBlock)
end
function SignalConnectionBlock:Respond(...)
	local freeConnectionThread = self.freeConnectionThread
	local connections = self.connections
	for i=1, #connections do
		local connection = connections[i]
		if not connection then
			table.remove(self.connections, i)
		end
		if connection.isActive then
			if not freeConnectionThread then
				self.freeConnectionThread = coroutine.create(ConnectedSignalHandler)
				coroutine.resume(self.freeConnectionThread, self)
			end
			task.spawn(self.freeConnectionThread, connection.func, ...)
			
			if self.once then
				connection:Disconnect()
			end
		end	
	end
end
function SignalConnectionBlock:AddConnection(func)
	self.counter += 1
	local new_connection = SignalConnection.new(func, self)
	if self.counter > 1 then
		if #self.removedIndices > 0 then
			self.connections[table.remove(self.removedIndices, 1)] = new_connection
			return new_connection
		end 
	end	
	local connection_index = #self.connections + 1
	new_connection.index = connection_index
	self.connections[connection_index] = new_connection

	return new_connection
end
function SignalConnectionBlock:isFull()
	return self.counter == self.size
end
function SignalConnectionBlock:isFree()
	return self.counter < self.size
end

function Signal.new(blockSize: number?)
	--local self = setmetatable({}, {
	--	_head = nil,
	--	_tail = nil,
	--	blockSize = blockSize or 15,
	--	waitConnections = {},
	--	isEmpty = true,
	--	counter = 0,
	--	__index = function(t, k)
	--		if debug.info(2, "s") == traceback_id then
	--			return getmetatable(t)[k]
	--		end
	--		return Signal[k]
	--	end,
	--	__newindex = function(t, k, v)
	--		if debug.info(2, "s") ~= traceback_id then
	--			error(string.format("Attempt to set SignalConnection.%s in a read-only table",tostring(k)), 2)
	--		end
	--		rawset(getmetatable(t), k ,v)
	--	end,
	--})

	local self = {
		_head = nil,
		_tail = nil,
		blockSize = blockSize or 15,
		waitConnections = {},
		isEmpty = true,
		connectedCounter = 0,
	}

	return setmetatable(self, Signal)
end

function Signal:Fire(...)
	
	
	local current_block = self._head
	while current_block ~= nil do		
		current_block:Respond(...)		
		current_block = current_block._next
	end

	for _, thread in ipairs(self.waitConnections) do
		coroutine.resume(thread, ...)
	end


end
function Signal:Wait()
	self.connectedCounter += 1
	table.insert(self.waitConnections, coroutine.running())
	return coroutine.yield()
end

function Signal:Connect(func): SignalConnection
	self.connectedCounter += 1
	local isEmpty = self.isEmpty
	local blockSize = self.blockSize
	if isEmpty then
		local signalconnectionblock = SignalConnectionBlock.new(blockSize)
		self._head , self._tail = signalconnectionblock, signalconnectionblock
		self.isEmpty = false
	end
	
	local current_block = self._head
	if current_block._next == nil and current_block:isFull() then
		local connection_block = SignalConnectionBlock.new(blockSize)
		self._tail, current_block._next = connection_block, connection_block
		connection_block._prev = current_block
		return connection_block:AddConnection(func)
	elseif current_block:isFree() then
		return current_block:AddConnection(func)
	end
	

	while current_block._next ~= nil do
		current_block = current_block._next
		if current_block:isFree() then
			return current_block:AddConnection(func)
		end
	end

	local connection_block = SignalConnectionBlock.new(blockSize)
	self._tail._next = connection_block
	connection_block._prev, self._tail = self._tail, connection_block
	
	return connection_block:AddConnection(func)	
	
end


function Signal:Once(func): SignalConnection
	self.connectedCounter += 1
	local isEmpty = self.isEmpty
	local blockSize = self.blockSize
	if isEmpty then
		local signalconnectionblock = SignalConnectionBlock.new(blockSize, true)
		self._head , self._tail = signalconnectionblock, signalconnectionblock
		self.isEmpty = false
	end

	local current_block = self._head
	if current_block._next == nil and current_block:isFull() then
		local connection_block = SignalConnectionBlock.new(blockSize, true)
		self._tail, current_block._next = connection_block, connection_block
		connection_block._prev = current_block
		return connection_block:AddConnection(func)
	elseif current_block.once and current_block:isFree() then
		return current_block:AddConnection(func)
	end

	while current_block._next ~= nil do
		current_block = current_block._next
		if current_block.once and current_block:isFree() then
			return current_block:AddConnection(func)
		end
	end

	local connection_block = SignalConnectionBlock.new(blockSize, true)
	self._tail._next = connection_block
	connection_block._prev, self._tail = self._tail, connection_block

	return connection_block:AddConnection(func)	
	
end
function Signal:GetConnectedCounter(): number
	return self.connectedCounter
end

return Signal :: SignalModule

I thought of a workaround that I could let the “read-only” table that will be returned to the user by the table proxy. This way I can avoid using the debug.info == traceback_id judgment to waste time.