Best way to make a function for various frames? (for this case)

Hello :hugs:
I’m sorry if this title seems weird, It’s hard to explain what I’m trying to accomplish with this. So, I’ll try my best!

Context: I’m trying to do the most minimum amount of typing in my scripting, so I’m trying to use this as an opportunity in order to try to progress with my programming while trying to follow the DRY principal religiously.

Therefore, I apologize if what I’m saying or suggesting doing might making no sense, but my goal is trying to find an easy and less long way to connect a bunch of things, I’m making a new version of my avatar editor that I’ve made last year. How it works is things are loaded after the frames have a child added function, and it fires an identification string with an array filled with information.

The scripting:
Info: var is a module I used to contain all the variables, EditorDataMod is just an info thing for containing which items are equipped or not for limit tracking.

ok now I want to try to connect this with a bunch of other frames, but how would I go about this? I want to connect it for when the child is added and detect for when it’s returned. So I’ve made this:

 function BaseUI.ButtonPressedExecuted(Button)
	if Button.ClassName ~= "ImageButton" then return end
	function ButtonPressed()
		var.SoundStorage.Click2:Play()
		return Button, var.EditorDataMod:InsertObject(Button.Name, MaxNumber)
	end
  end

var.ScrollingFrames.Clothes.Roblox.ChildAdded:Connect(var.BaseUIMod.ButtonPressedExecuted)

But, with that I don’t know how I would pass the max number or how I would be able to connect for when it has returned.

Another potential option I had is this, which is passing a function as a variable:

function BaseUI.ButtonHasBeenPressed(Frame,Max,FunctionToExecute)
	Frame.ChildAdded:Connect(function(Button)
		if Button.ClassName ~= "ImageButton" then return end
		var.SoundStorage.Click2:Play()
		FunctionToExecute(Button, var.EditorDataMod:InsertObject(Button.Name, Max))
	end)
end

var.BaseUI.ButtonHasBeenPressed(var.ScrollingFrames.Clothes.Roblox,1, function(Btn, Result)
	--do stuff 
end)

What I’m trying to say: I’m confused on which is the best method to make a function that can connect to various frames. If you have something else please mention, I’m also curious to know if there’s another way you guys do stuff that might be better than this. I’m also curious to know if the second one can work since I’m very unsure on passing functions as variables.

Thanks for reading this far, I’m sorry if it’s too long! Have a good day :blush:

1 Like