Morph giver feedback

I made a OOP morph giver, but I am not sure if it is good, if you can send feedback on what to change, that would be great!

Server:

local MorphHandlerModule = require(game.ReplicatedStorage:WaitForChild("MorphHandler"))
local EquipMoprh = game.ReplicatedStorage:WaitForChild("EquipMorph")

EquipMoprh.OnServerEvent:Connect(function(Player, MorphPrompt)
	local Character = Player.Character
	local Humanoid = Character:FindFirstChild("Humanoid")
	
	local MorphHandler = MorphHandlerModule.New(Character, MorphPrompt)
	
	MorphHandler:WeldMorph()
	
	Humanoid.Died:Connect(function()
		MorphHandler:SetUntrue()
	end)
end)

Module script:

local MorphHandler = {}
MorphHandler.__index = MorphHandler

function MorphHandler.New(character, morphType)
	local self = setmetatable({}, MorphHandler)

	self.Character = character
	self.Shirt = character:WaitForChild("Shirt")
	self.Pants = character:WaitForChild("Pants")
	self.MorphType = morphType
	self.MorphEquipped = false

	return self 
end


function MorphHandler:SetUntrue()
	self.MorphEquipped = false
end

function MorphHandler:WeldMorph()
	if not self.MorphEquipped then 

		local BasePartTable = {}

		for _, BasePart in ipairs(self.Character:GetChildren()) do 
			if BasePart:IsA("BasePart") then 
				table.insert(BasePartTable, BasePart)		
			end
		end

		for _, Hats in ipairs(self.Character:GetChildren()) do 
			if Hats:IsA("Accessory") then 
				Hats:Destroy()
			end
		end

		self.Shirt.ShirtTemplate = self.MorphType.Shirt.ShirtTemplate
		self.Pants.PantsTemplate = self.MorphType.Pants.PantsTemplate

		for _, BaseParts in ipairs(self.MorphType:GetChildren()) do 
			if BaseParts:IsA("Model") then 
				for _, Bases in ipairs(BasePartTable) do
					local ClonedBasePart = BaseParts:Clone()
					if Bases.Name == ClonedBasePart.Name then 

						ClonedBasePart.PrimaryPart.CFrame = Bases.CFrame

						local Weld = Instance.new("Weld")
						Weld.Part0 = Bases
						Weld.Part1 = ClonedBasePart.PrimaryPart
						ClonedBasePart.Parent = Bases
						Weld.Parent = ClonedBasePart.PrimaryPart

						for _, PartsInBase in ipairs(ClonedBasePart:GetChildren()) do 
							PartsInBase.Anchored = false
						end
					end
				end
			end
		end

		self.MorphEquipped = true
	end
end

return MorphHandler

Client:

local EquipMoprh = game.ReplicatedStorage:WaitForChild("EquipMorph")

local function GetMorphType()
	for _, Morphs in ipairs(workspace.Morphs:GetChildren()) do 
		for _, EquipMorphButton in ipairs(Morphs:GetChildren()) do 
			if EquipMorphButton.Name == "EquipMorph" then
				return EquipMorphButton
			end
		end
	end
end

local EquipMorphButton = GetMorphType()

EquipMorphButton.ProximityPrompt.Triggered:Connect(function()
	EquipMoprh:FireServer(EquipMorphButton.Parent)
end)
3 Likes

If your morph system is limited to this sole piece of code, then you are just misusing OOP. Creating an instance that does not need to conserve its own state through time (because you are never reusing the MorphEquipped property, nor the others) has no purpose. Simple functions would be the right way to do it.

However, I think you wanted to have the MorphHandler instance persist in time for the attached Character, or actually the attached Player would be more appropriate imo. To achieve this, you could use memoization, but a simpler way is to store your instances in a table, using the Player as a key.

Furthermore, you create a memory leak with your Humanoid.Died connection.