Cleaning up a child class

-- Main class (In another module script)
--// Create tycoon class
local Tycoon = {}
Tycoon.__index = Tycoon

--// Create tycoon object
function Tycoon.new(player, spawnPoint)
	local self = setmetatable({}, Tycoon)
	
	self.Owner = player
	self._spawn = spawnPoint
	
	--// CONFIGS
	self._convertRate = 1 -- Seconds it takes to convert egg to gold (s)
	self._spawnRate = 5 -- Dragon egg spawn rate (s)
	self._maxEggs = 100 -- Max eggs that can exist per plot
	
	--// CONSTANTS
	self._mergeAmount = 3 -- Dragons it takes to upgrade to next tier
	self.TotalEggs = 0
	self.LastTouched = 0
	
	return self
end

function Tycoon:LoadComponents()
	for _, instance in ipairs(self.Model:GetDescendants()) do
		for _, componentScript in ipairs(componentFolder:GetChildren()) do
			if CollectionService:HasTag(instance, componentScript.Name) then
				local compModule = require(componentScript)
				local newComp = compModule.new(self, instance, self.Owner)
				newComp:Init()
			end
		end
	end
end

function Tycoon:WaitForExit()
	PlayerManager.PlayerRemoving:Connect(function(player)
		if self.Owner == player then
			self:Destroy()
		end
	end)
end

function Tycoon:Destroy()	
	self.Model:Destroy()
	self._clientReplication:Disconnect()
	self._eggRemoval:Disconnect()
	self.plantChanged:Disconnect()
	self._spawn:SetAttribute("Occupied", false)
end

--Child class (In a different module script)
local AutoCollect = {}
AutoCollect.__index = AutoCollect

function AutoCollect.new(tycoon, instance, player)
	local self = setmetatable({}, AutoCollect)
	
	self.Tycoon = tycoon
	self.Instance = instance
	self.Owner = player
	self.Model = self.Tycoon.Model
	
	return self	
end

function AutoCollect:Init()
	--self:InitializeAutoCollect()
	self:OnTouched()
end

function AutoCollect:OnTouched()
	self.Instance.PrimaryPart.Touched:Connect(function(hitPart)
		local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)
		if self.Owner == player then
			self.Model.Essentials.AutoCollector.Transparency = 0.5
			self.Model.Essentials.AutoCollector.CanTouch = true
		end
	end)
end

function AutoCollect:InitializeAutoCollect()
	if self.Owner:IsInGroup(10600473) then
		self.Model.Essentials.AutoCollector.Transparency = 0.5
		self.Model.Essentials.AutoCollector.CanTouch = true
	end
end

function AutoCollect:CleanUp()
	print(self)
end

return AutoCollect

I’m using a component pattern design for my tycoon by following a youtube tutorial. I’m leaving some code out as its not as relevant. So far it seems organized however, I need help on how to clean up my main class and child class. Is there any way I can hook/connect all my child classes (I have multiple) into my main Tycoon class and just clean up everything altogether? Any help is appreciated. I apologize if anything is unclear. I have the Tycoon class in one module script and the AutoCollector class in another module script.

1 Like

Seems clean enough to me, although usually the term “child class” means a class who inherits from some “parent class”.

This is just two different classes

2 Likes

Sorry if it was confusing, I have the Tycoon class in one module script and the AutoCollect class in another module script. I don’t believe that I’m cleaning up the class though, especially the child class as it’s only printing right now. How can I clear all references and connections for both classes easily? Is there a way I can hook/connect the child classes (I have multiple of these) to the main Tycoon script so I can just clean it all up together?

1 Like