Am I doing OOP Correctly?

So I made a part go up, I have a couple of questions though

Do you make the part inside of the constructor function, for example do instance.new()

Also instead of using game.Workspace:FindFirstChild(“Part”), what other alternatives can I use to refernce the part I just created in my constructor function

-- MODULE SCRIPT 
Part =  {}
Part.__index = Part

--Services 
local tweenService = game:GetService("TweenService")

function Part.new(color, size, position)
	local newPart = {}
	setmetatable(newPart, Part)
	
	newPart.Color = color 
	newPart.Size = size 
	newPart.Position = position
	
	local part = Instance.new("Part")
	part.Anchored = true
	part.Parent = workspace
	part.Color = color 
	part.Name = "Part"
	part.Size = size 
	part.Position = position
	
	
	return newPart 
end

function Part:Move()
	local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear)
	local part = game.Workspace:FindFirstChild("Part")
	local tween = tweenService:Create(part, tweenInfo, {Position = part.Position * Vector3.new(0, 10, 0)})
	tween:Play()
	
end

return Part

--SERVER SCRIPT 
local rs = game:GetService("ReplicatedStorage")
local Part = require(rs:WaitForChild("Part"))

local newPart = Part.new(Color3.fromRGB(255, 0, 0), Vector3.new(3, 3, 3), Vector3.new(3,3,3))
newPart:Move()


This is all it does

You can just store the created part in the table as something like:

function Part.new(color, size, position)
	local newPart = {}
	setmetatable(newPart, Part)
	
	newPart.Color = color 
	newPart.Size = size 
	newPart.Position = position
	
	local part = Instance.new("Part")
	part.Anchored = true
	part.Parent = workspace
	part.Color = color 
	part.Name = "Part"
	part.Size = size 
	part.Position = position
	
	newPart.Part = part
	
	return newPart 
end

Then instead of searching the workspace for the first brick named ‘Part’ you can reference it directly:

function Part:Move()
	local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear)
	local tween = tweenService:Create(self.Part, tweenInfo, {Position = self.Position * Vector3.new(0, 10, 0)})
	tween:Play()
	
end
1 Like

thank you this is going to help me alot