How do I shorten repeated similar functions?

Hi there fellow developers.
I have a code that does work but it takes up way to much space, here it is:

Module.Button1.ButtonGUI.Activated:Connect(function()
	
	if Money.Value - Module.Button1.Price <= -1 then
		return
	else
		Money.Value -= Module.Button1.Price
		return
	end
	
end)

Module.Button2.ButtonGUI.Activated:Connect(function()

	if Money.Value - Module.Button2.Price <= -1 then
		return
	else
		Money.Value -= Module.Button2.Price
		return
	end

end)

Module.Button3.ButtonGUI.Activated:Connect(function()

	if Money.Value - Module.Button3.Price <= -1 then
		return
	else
		Money.Value -= Module.Button3.Price
		return
	end

end)

instead of repeating the function mulitple times, could i just do something like for example; Module.Button[1-3].ButtonGUI.Activated?

thanks!

local Buttons = {
   "Button1",
   "Button2",
   "Button3"
}

local function RemoveMoney(holder)
   if Money.Value - holder.Price <= -1 do
      return
   else
      Money.Value -= holder.Price
   end
end

for i = 1, #Buttons do
   local Holder = Module:FindFirstChild(Buttons[i])
   Holder.ButtonGUI.Activated:Connect(RemoveMoney)(Holder)
end

Maybe something like this could work, alternatively you could just loop through module and ofc have the right checks and should work out the same.

could be shorter

local function RemoveMoney(Holder)
  Money.Value -= (Money.Value - Holder.Price <= -1) and Holder.Price or 0
end

for _, ButtonHolder in Module:GetChildren() do
  if not ButtonHolder:IsA("Frame") then continue end -- Assuming the buttons are Frames holding TextButtons
  -- You can't pass arguments like how you did
  -- Rename ButtonGui -> Button
  ButtonHolder.Button.Activated:Connect(function() RemoveMoney(Button) end)
end

Hi there TheDCraft,
you can’t find the first child of a modulescript,
pls help

What is module referring too? I assumed it was a variable that was already defined?

Yes so the module is the modulescript table thats in a module script that’s inside replicated storage,
this is what the code in the modulescript looks like:


local ScreenGui = game.Players.LocalPlayer.PlayerGui.Shop
local ButtonsFolder = ScreenGui.Frame:WaitForChild("Buttons")

local module = {
	
	Button1 = {
		ButtonGUI = ButtonsFolder:WaitForChild("1");
		Price = 25
	};
	Button2 = {
		ButtonGUI = ButtonsFolder:WaitForChild("2");
		Price = 45
	};
	Button3 = {
		ButtonGUI = ButtonsFolder:WaitForChild("3");
		Price = 70
	};
	Button4 = {
		ButtonGUI = ButtonsFolder:WaitForChild("4");
		Price = 230
	};
	Button5 = {
		ButtonGUI = ButtonsFolder:WaitForChild("5");
		Price = 520
	};
	Button6 = {
		ButtonGUI = ButtonsFolder:WaitForChild("6");
		Price = 1700
	};
	Button7 = {
		ButtonGUI = ButtonsFolder:WaitForChild("7");
		Price = 4800
	};
	Button8 = {
		ButtonGUI = ButtonsFolder:WaitForChild("8");
		Price = 12200
	};
	Button9 = {
		ButtonGUI = ButtonsFolder:WaitForChild("9");
		Price = 90842357902437
	}
	
}

return module

and the script with the buttonactivated is a localscript

Oh then just loop through module?

local function RemoveMoney(Holder)
    if Money.Value - holder.Price <= -1 do
      return
    else
      Money.Value -= holder.Price
    end
end

for ButtonName, ButtonInfo in module do
   local Button = ButtonInfo.ButtonGUI

   Button.Activated:Connect(function() 
      RemoveMoney(Button) 
   end)
end
1 Like