Make multiple buttons work in as little code as possible

i want to switch to different uis depending on the button name, in this scrolling frame i plan to have lots of buttons, and each button would get it’s own ui. I dont feel like writing out individual MouseButton1Click functions for every single button, so how could I do that for all of them ? The target Gui is the same name as the button’s name.

What i have here does not work.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SwitchUI = require(ReplicatedStorage:WaitForChild("SpriteService", 2).Animation.SwitchUI)

local ScrollContainer = script.Parent.ScrollContainer
local Buttons = ScrollContainer:GetChildren()

for _, frame in pairs(Buttons) do
    if frame:IsA("Frame") then
        local TargetUI = ScrollContainer:FindFirstChild(frame.Name)
        frame.Button.MouseButton1Click:Connect(SwitchUI.SwitchFromHome(script.Parent, TargetUI)) -- I want to run the MouseButton1Click event on all buttons in as little code as possible instead of doing every function separately.
    end
end

SwitchFromHome is just a tween function.

Not sure if this is even possible but id like to try.

also the proposed code is supposed to be a replacement for this. (this is not the actual code.)

-- example
ScrollContainer.FreeSprites.Button.MouseButton1Click:Connect(function()
    SwitchUI.SwitchFromHome(script.Parent, ScrollContainer:FindFirstChild(ScrollContainer.FreeSprites.Name))
end)

ScrollContainer.hhhhhhhhh.Button.MouseButton1Click:Connect(function()
    SwitchUI.SwitchFromHome(script.Parent, ScrollContainer:FindFirstChild(ScrollContainer.hhhhhhhhh.Name))
end)

ScrollContainer.asdasdasd.Button.MouseButton1Click:Connect(function()
    SwitchUI.SwitchFromHome(script.Parent, ScrollContainer:FindFirstChild(ScrollContainer.asdasdasd.Name))
end)

ScrollContainer.dfhgdfgh.Button.MouseButton1Click:Connect(function()
    SwitchUI.SwitchFromHome(script.Parent, ScrollContainer:FindFirstChild(ScrollContainer.dfhgdfgh.Name))
end)

ScrollContainer.fdggffgd.Button.MouseButton1Click:Connect(function()
    SwitchUI.SwitchFromHome(script.Parent, ScrollContainer:FindFirstChild(ScrollContainer.fdggffgd.Name))
end)

ScrollContainer.dgfgdfgdfs.Button.MouseButton1Click:Connect(function()
    SwitchUI.SwitchFromHome(script.Parent, ScrollContainer:FindFirstChild(ScrollContainer.dgfgdfgdfs.Name))
end)

-- etc.
2 Likes
local c = ScrollContainer 
--you can also use c:GetChildren() to fetch all children of c
--to only keep buttons you can run a v:IsA("TextButton") check for each child
--but for this scenario, due to the low number of examples, we can hardcode:
local buttons = {c.FreeSprites, c.hhhhhhhhh, c.asdasdasd, c.dfhgdfgh, c.fdggffgd, c.dgfgdfgdfs}

for _, button in pairs(buttons) do
	button.Button.MouseButton1Click:Connect(function()
		SwitchUI.SwitchFromHome(script.Parent, button)
	end)
end

Here’s how you can filter the buttons(in case you want to fetch them dynamically):

local function filter(button: Instance): boolean
	--you can customize this as you wish, but it must return true or false
	return button:IsA("TextButton")
end

local function getButtons(): {Instance}
	local buttons = {}
	for _, v in pairs(c:GetChildren()) do
		if filter(v) then table.insert(buttons, v) end
	end
	return buttons 
end
1 Like
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SwitchUI = require(ReplicatedStorage:WaitForChild("SpriteService").Animation.SwitchUI)
local ScrollContainer = script.Parent.ScrollContainer
local Buttons = ScrollContainer:GetChildren()

for _, frame in pairs(Buttons) do
    if frame:IsA("Frame") then
        local button = frame:FindFirstChildOfClass("TextButton")
        if button then
            button.MouseButton1Click:Connect(function()
                local TargetUI = script.Parent:FindFirstChild(button.Name)
                if TargetUI then
                    SwitchUI.SwitchFromHome(script.Parent, TargetUI)
                end
            end)
        end
    end
end

May not be totally correct but this is how you could do that. Didn’t test anything so … Just running down the frame setting up Connect(function()'s for each button in one for next loop. Once you get it it will do this fine.

@2112Jay @NyrionDev Im trying a mixture of both of your solutions right now, still need to polish off some of the actual backend tweening

1 Like

Solution compiled via mixing the two code blocks above this reply…
Modify according to your needs.

local Container = -- path to your container. The target UI should be parented under it.
local ScrollContainer = -- path to your scrollingframe or frame, rename variable accordingly

local Buttons = {
	-- add button paths here. should be text/imagebuttons.
}

for _, button in pairs(Buttons) do
	local TargetUI = -- path to your ui
	
	if TargetUI then
		button.MouseButton1Click:Connect(function()
			-- insert your switch function/code
		end)
	else
		warn(`Frame {button.Name} does not exist.`) -- devforum doesn't support string interpolation for backticks, so this doesn't look right
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.