How to use event connections and OOP?

I tried to look everywhere but i can’t find answer, what is the best way to add events to OOP, both roblox’s ones and your custom?

1 Like

Usually like this:

You can replace the bindable event with another signal module like GoodSignal if you want to.

3 Likes

you can write a FunctionPool/Signal module yourself if you want, but there are some open sourced ones, I use the signal module from sleitnick’s package

local Signal = require(path)

local module = {}
module.__index = module

function module.new() -- let's make a zombie follow a path
   self.Model = ...

   self.EndReached = Signal.new() -- name it whatever it's your custom event
   self.EndReached:Connect(function(attributes)
      print("Custom event called")
   end)

   self.ZombieDied = Signal.new()
   self.ZombieDied:Connect(function(
      print("Zombie died")
   end)

   self.Model.Humanoid.Died:Connect(function(
      self.ZombieDied:Fire()
   end)
end

function module:moveZombie()
   -- move code

   -- when it reaches the end call
   self.EndReached:Fire(attributes) -- This will fire the print
end

1 Like

I want to know how write it myself, but thx

local Pool = {}
Pool.__index = Pool

function Pool.new(MaxConnections : number?)
	local self = setmetatable({}, Pool)

	self._Functions = {} -- stores all of the functions that are connected to the signal (ex. when you fire the signal, it will fire all of the functions inside of this, if you have a lot of event:Connect() happening for the same event

	return self
end

function Pool:Connect(Function)
	if typeof(Function) == "function" then
		table.insert(self._Functions, Function)
	else
		error("Function Pool: Passed argument is not a function.")
	end
end

function Pool.__call(self, ...) 
	for _, Function in self._Functions do
		Function(...)
	end
end

-- the __call metamethod makes it so you can call the signal as if it were a function, the "..." are the arguments being passed
1 Like

Those are not signals, i mean connections, for example Touched, Bindable Events, Activated et.

Im not the best at OOP, but this is what I usually do.

Module:

local module = {}
module.__index = module

function module.new(TheString : string, Event : RemoteEvent)
	local self = setmetatable({}, module)
	self.String = TheString
	self.Event = Event
	return self
end
	
function module:FireEvent()
	self.Event:FireServer(self.String)
end

return module

Local Script:

local module = require(script.ModuleScript)

local TheString = module.new("Hello!", game.ReplicatedStorage.RemoteEvent)

TheString:FireEvent()

Server Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, String)
	print(String)
end)
1 Like

But what about normal connections such as touched or activated?

Module:

local module = {}
module.__index = module

function module.new(Part : BasePart)
	local self = setmetatable({}, module)
	self.Part = Part
	return self
end
	
function module:OnTouched()
	self.Part.Touched:Connect(function()
		print(self.Part.Name.." was touched!")
	end)
end

return module

server script:

local module = require(script.ModuleScript)

local Part = module.new(workspace.SpawnLocation)

Part:OnTouched()

You can do the onTouched event inside the class itself, but I prefer to do it like this.

1 Like

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