OOP Practice Help

Hi! I’m currently learning about OOP, and I made these modules by myself. I wanted to ask if I wrote this in good practice, or if there are better ways of doing this. The code is working, when I call Light.new(object) in a script, it adds a Proximity Prompt to that part, which when triggered makes the part’s material neon.

I tried to go for a composition style rather than inheritance, thinking that a light has a proximity prompt. Don’t know if it was the best approach though.

What other things could I try to write using OOP to practice more?

Proximity module:

local Proximity = {}
Proximity.__index = Proximity

function Proximity.new(holdDuration)
	local self = setmetatable({}, Proximity)

	self.HoldDuration = holdDuration
	self.ActionText = "Activate me!"
	
	return self
end

function Proximity.Activate(object, changes, player)
	for i, change in changes do
		print(i,change)
		object[i] = change
	end
	print(player.Name .. " triggered " .. object.Name .. " proximity prompt ")
end

return Proximity

Light module:

local Proximity = require(script.Parent.Proximity)

local Light = {}
Light.__indes = Light

local changes = {
	Material = Enum.Material.Neon,
}

function Light.new(object)
	local self = setmetatable({}, Light)
	
	self.Prompt = Proximity.new(1)
	
	local int = Instance.new("ProximityPrompt")
	int.HoldDuration = self.Prompt.HoldDuration
	int.ActionText = self.Prompt.ActionText
	int.Parent = object
	
	int.Triggered:Connect(function(player)
		Proximity.Activate(object, changes, player)
	end)
	
end

return Light

You’re on the right track with using composition, and I think it’s a good call for this kind of modular setup. Having Light use Proximity instead of inheriting from it makes sense since a light “has a” prompt rather than “is a” prompt.

A couple quick suggestions:

  • In the Light module you’re missing a return self at the end of Light.new so you won’t be able to access any properties later.
  • Also looks like there’s a typo: __indes should be __index.
  • Might be worth storing changes as part of the Light instance so you can configure it per light object.

If you want to practice you can try making a simple inventory system where each item has its own module and behavior. It’ll help you get more comfortable with composition vs inheritance and when to use which.