Help with OOP inside a module script

So I was working on a simple OOP module script that generates a Tohunga (Tohunga is just a species for one of my games), but the script just changes the base Tohunga model, when it’s supposed to clone the model, then change aspects of the cloned Tohunga model.


NOTE 1: This is my first use of OOP inside of roblox, so there are probably a lot of issues of the script that I haven’t figured out, I would rather you guys not modifiy my script, since I would like to learn more about OOP via coding, and not look at other people’s scripts, but if there are stuff like memory leaks, bad practices ect, then please notify me where they are occuring, and how to fix them.


Module script, inside of ServerScriptService:

local Tohunga = {}

Tohunga.__index = Tohunga

local partTypes = {
	[1] = "BasePart",
	[2] = "MeshPart",
	[3] = "UnionOperation" 
	
}

function setTohungaEyeStock(Model,EyeStockColor)
	Model.FullHead.Head.EyeStock.Color = EyeStockColor
	if Model.FullHead.Head.EyeStock:IsA(partTypes[3]) then
		Model.FullHead.Head.EyeStock.UsePartColor = true
	end
end

function setTohungaColor(Model,MainColor,SideColor,MaskColor)
	for _,part in pairs(Model.UpperBody:GetDescendants()) do
		if part:IsA(partTypes[1]) or part:IsA(partTypes[2]) or part:IsA(partTypes[3]) then
			part.Color = MainColor
			if part:IsA(partTypes[3]) then
				part.UsePartColor = true
			end
			continue
		end
	end
	
	for _,part in pairs(Model.LowerBody:GetDescendants()) do
		if part:IsA(partTypes[1]) or part:IsA(partTypes[2]) or part:IsA(partTypes[3]) then
			part.Color = SideColor
			if part:IsA(partTypes[3]) then
				part.UsePartColor = true
			end
			continue
		end
	end
	
	Model.FullHead.Mask.Color = MaskColor
	if Model.FullHead.Mask:IsA(partTypes[3]) then
		Model.FullHead.Mask.UsePartColor = true
	end
end 

-- vv This part of the script is where the main issue is happening. vv

function Tohunga.GenerateTohunga(Model,Name,MainColor,SideColor,MaskColor,EyeStockColor,Anchored)
	local newTohunga = {}
	setmetatable(newTohunga,Tohunga)
	
	newTohunga.Model = Model
	newTohunga.Model.Name = Name
	newTohunga.Model.MainColor.Value = MainColor
	newTohunga.Model.SideColor.Value = SideColor
	newTohunga.Model.MaskColor.Value = MaskColor
	newTohunga.Model.EyeStockColor.Value = EyeStockColor
	
	coroutine.wrap(function()
		setTohungaColor(Model,MainColor,SideColor,MaskColor)
	end)()
	
	coroutine.wrap(function()
		setTohungaEyeStock(Model,EyeStockColor)
	end)()
	
	for _,part in pairs(newTohunga.Model:GetDescendants()) do
		if part:IsA(partTypes[1]) or part:IsA(partTypes[2]) or part:IsA(partTypes[3]) then
			coroutine.wrap(function()
				part.Anchored = Anchored
			end)()
		end
	end
	
	return newTohunga
end

-- ^^ Right above here is the function where the main issue is happening. ^^

function Tohunga:AddDialog(Dialog)
	local dialogClone = Dialog:Clone()
	dialogClone.Parent = self.Model.FullHead.Mask
end

function Tohunga:ChangeDialog(Dialog)
	local dialogClone = Dialog:Clone()
	
	for _,dialog in pairs(self.Model.FullHead.Mask:GetChildren()) do
		if dialog:IsA("Dialog") then
			dialog:Destroy()
			continue
		end
	end
	
	dialogClone.Parent = self.Model.FullHead.Mask
end


function Tohunga:SetTohungaParent(Parent)
	self.Model.Parent = Parent
end

return Tohunga

NOTE 2: I know I never cloned the model in the module script, the reason why I didn’t is because I couldn’t figure out how I should go about cloning the model in a readable fashion, that also didn’t result in anything odd occurring.

Any help is appreciated!

You said this is in a server script but in the title it states the code is in a module script?

1 Like

The original titled just made the script sound like it was a normal script, even though it was a module script, I changed the titled to match the situation more closely, but I forgot to change that part. The script is a module script, I’ll go and edit that part to say module script, sorry.

Why do you need OOP for this? It seems a bit unnecessary.

I kind of just wanted to practice my OOP skills, since I only started to learn OOP very recently.

@ScarletHypernova Are you still here?

Have you tried using :Clone()?

I think here you would use

newTohunga.Model = Model:Clone()

instead.

1 Like

I have tried doing that a couple of times, and it always did odd stuff such as moving the stock Tohunga into workspace, and having the cloned Tohunga stay in server storage. A few minutes ago, I tried to clone the stock Tohunga model inside the server script that generates the Tohunga, not the module script, and that seems to have fixed my issue, thank you for replying though. Also, yes I did attempt to clone the model inside the module script, but I never could find a good way to do it that doesn’t result in something messing up.