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.)
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
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.
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