What would be the best way to add a function to a group of objects?

I am working on rewriting the code for my game, The Room. It currently has a lot of bad practices, and I’m wondering what I should use in place of them. Currently, I have a collection of similar objects that behave the same way (for example, the GUI code below for the Home Screen). However, I know that there has to be a better way to calling the function for each individual object. I have considered using collection service, but I have heard a lot of debate as to whether I should use OOP or not. What would be the best strategy for this scenario? Any and all feedback is appreciated!

This is the code now.

		
		local NewColor = Color3.new(0.545098, 0.545098, 0.545098)	--Define Color Codes
		local OldColor = Color3.new(1, 1, 1)
		
		local function MouseEntered(GuiObject)
			GuiObject.TextColor3 = NewColor
		end
		
		local function MouseLeaved(GuiObject)
			GuiObject.TextColor3 = OldColor
		end
		
		PlayButton.MouseEnter:Connect(function()
			MouseEntered(PlayButton)
		end)		--enact hovers
		
		EndingsButton.MouseEnter:Connect(function()
			MouseEntered(EndingsButton)
		end)
		
		UpdateLogButton.MouseEnter:Connect(function()
			MouseEntered(UpdateLogButton)
		end)
		
		PlayButton.MouseLeave:Connect(function()
			MouseLeaved(PlayButton)
		end)
		
		EndingsButton.MouseLeave:Connect(function()
			MouseLeaved(EndingsButton)
		end)
		
		UpdateLogButton.MouseLeave:Connect(function()
			MouseLeaved(UpdateLogButton)
		end) 

(This comes up a lot in my game. For example, I have a function for picking up toys, and for each individual toy, I call the function.

2 Likes

In a simple way, you can do the following:

local objects = {
    TextButton;
    TextButton;
}

for obj in ipairs(objects) do

  obj.MouseEnter:Connect(function() end)
  obj.MouseLeave:Connect(function() end)
  --code that applies to a group of objects goes here

end

If you’re looking for a more object-oriented approach, you can do the following:

--module script

local m = {}
m.__index = m

function m.new(obj)
    local self = setmetatable({
        --like this if you want to reference a connection
        hoverConnection = obj.MouseEnter:Connect(function() end);
    }, m)
    
    --or simply:
    obj.MouseEnter:Connect(function() end)
    obj.MouseLeave:Connect(function() end)

    return self
end

return m
--localscript

local textButton = Instance.new("TextButton")

--this will create a new object and also create those connections
local customTextButtonObj = require(module).new(textButton)

The simplest way for you would be the first type. This is because it would be the easiest to do with a less amount of code. For the second type, you essentially need to create a new custom object every time you create a new Instance. You might as well just end up using the second option and using the first way to create those objects. This really depends on your situation and what you decide.

2 Likes

I think I will use the first option, thanks! I (can’t believe it was this simple all along!)