Multiple Button Scripting Help

You can write your topic however you want, but you need to answer these questions:

  1. I want to make a more proficient code when it comes to Making buttons how would i code this better? I know about i,v in pairs but what exactly do i need to do?
  2. What is the issue? Include screenshots / videos if possible!
    This Seems not effective and slow and not good looking code
  3. What i can mainly think of is going through for i,v in pairs and checking for the buttons name is this then do that but i’m not exactly sure if this is the best option
TextButton.MouseButton1Click:Connect(function()
Print("Opens Inventory")
end)
Textbutton2.MouseButton1Click:Connect(function()
print("Opens Settings")
end)
Textbutton3.MouseButton1:Connect(function()
Print("Open Shop Example")
end)

Well as Roblox taught me once;

for _, button in pairs(Frame:GetDescendants()) do
if button:IsA("TextButton") or button:IsA("ImageButton") then
if button.Name == "MatchingName" then
button.MouseButton1Click:Connect(function()
end)
end
end
end

I suppose that’s a way.

1 Like

One thing though you check is the matching name then you connect the function to the button don’t you have to copy and paste the mousebutton1click again to the other if statements?

You could also do

for _, button in pairs(Frame:GetDescendants()) do
if button:IsA("TextButton") or button:IsA("ImageButton") then
button.MouseButton1Click:Connect(function()
if button.Name == "MatchingName" then
end
end)
end
end

But other than copy and pasting, I suppose not.

1 Like