Am I doing OOP right?

local ISOS = require(game:GetService("ReplicatedStorage").ISOS)
local Button = ISOS.Class({})

function Button:__init(model: Model, position: Vector3, rotation: Vector3)
	local VisualModel = model:Clone()
	VisualModel:SetPrimaryPartCFrame(CFrame.new(position))
	VisualModel.Parent = workspace
	self.Model = VisualModel
	
	self.IsOn = false
	self.Input = false
	self.Output = false
	
	local Event = Instance.new("BindableEvent")
	self.Event = Event
	self.Listener = false
end

function Button:Click()
	local newState = not self.IsOn
	
	self:Disconnect()
	print("clicked once...")
	
	self.IsOn = newState
	self.Event:Fire(newState)
end

function Button:Listen(BindableEvent: RBXScriptSignal)
	self.Listener = BindableEvent:Connect(function()
		self:Click()
	end)
end

function Button:Disconnect()
	self.Listener:Disconnect()
	self.Listener = false
end
return Button

Is this correct way of making Listeners? or should i scrap this? also are there any memory leaks?

2 Likes

Seems alright but you might want to set self.Listener to either nil or the event instead of using false.

3 Likes

I can’t if i want it to be reusable, but after thinking i understood i can do that. thx for help

I think OOP is just another way to obfuscate code. :rofl:

Just glancing at that makes my brain hurt.

2 Likes

For me it’s speeding up your work by 400%, i decided to learn it as i saw opportunity to use it widely, and it’s great, i reccomend you to learn it

2 Likes

IDK if it matters, but I would use the GoodSignal module instead of bindable events. Otherwise, looks good

2 Likes

But this module is probably based on bindables too, soo i see no difference

Code looks fine in general but I don’t see a reason for defining the new state. I’d just use not self.IsOn twice instead of going for another local.

2 Likes

Ik, it was first time i made events soo yea, thx for feedback

1 Like

I’m pretty sure goodsignal is pure lua (meaning, no bindable events). I think it uses a table or something, to store the events

1 Like

Then how it uses them? what is their way to do that? i want to do it myself to learn and eventually change stuff for what i need

OOP is great when you know how to handle it.

Here’s my usage:


You can check out the source code for GoodSignal here:

Also, you seem to be understanding OOP and it’s fundamentals quite nicely, but if you ever need to know anything, you can check out this thread here:

Alternatively, I made a class module recently that makes OOP so much easier to do, consider giving it a look:

1 Like

It literally just runs functions lol and it’s super fast because of that

1 Like

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