Which is better for OOP

So I have a tool and a gun class with methods like :Equip() and :Unequip() like so, which i call in a regular script whenever the tool is equipped or unequipped.

local Gun = GunClass.new()
local Tool = script.Parent

Tool.Equipped:Connect(function()
	Gun:Equip()
end)

Tool.Unequipped:Connect(function()
	Gun:Unequip()
end)

But is it better to do it like above or should I pass the tool into the GunClass constructor and do it like below, where I only have to just create the gun object in the regular script and let the class do all the work

local GunClass = {}
GunClass.__index = GunClass

function GunClass.new(Tool)
	local self = setmetatable({}, GunClass)
	
	Tool.Equipped:Connect(function()
		self:Equip()
	end)
	
	Tool.Unequipped:Connect(function()
		self:Unequip()
	end)
	
	return self
end

function GunClass:Equip()
	print("Equipped")
end

function GunClass:Unequip()
	print("Unequipped")
end

return GunClass

If every weapon created uses the same events, it’s recommended to place them in the constructor to avoid duplicating the same two events, which can save time when you’re trying to change every single event.

1 Like

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