local btns = folder
for _, btn in btns:GetChildren() do
btn.Clicked:Connect(function()
-- "btn" would be the button instance
print(btn.Name.." was clicked")
end)
end
local frames = folder
for _, frame in frames:GetChildren() do
for _, btn in frame:GetChildren() do
btn.Clicked:Connect(function()
-- "btn" would be the button instance
print(btn.Name.." was clicked")
end)
end
end
If there are other types of instances in the frame other than buttons then you can use the :IsA() function to make sure it’s a button.
i get this error:
Clicked is not a valid member of TextButton “Players.Vortexuel560.PlayerGui.SpawnMenuWindow.SM.Main.Main.Battery.TextButton”
With this script:
local event = game.ReplicatedStorage.SMH
local frames = script.Parent.Main.Main
for _, frame in frames:GetChildren() do
if frame:IsA("Frame") then
for _, btn in frame:GetChildren() do
if btn:IsA("TextButton") then
btn.Clicked:Connect(function()
-- "btn" would be the button instance
print(btn.Parent.Name.." was clicked")
end)
end
end
end
end
I’d recommend utilizing :GetDescendants() for that if you want to retrieve all of the buttons within the given frames (especially if some are nested beyond the first layer of each frame).
Otherwise, if you know for sure that there will only be buttons on the first layer of those frames, you could continue using two :GetChildren() loops
An event with the name of “Clicked” currently does not exist for TextButtons. The primary events that are used to detect if a GuiButton has been pressed are Activated or MouseButton1Click.
Example Revision
local parentObject = script.Parent
local Main1 = parentObject:WaitForChild("Main")
local Main2 = Main1:WaitForChild("Main")
local function buttonFunctionality(object)
if not object:IsA("GuiButton") then return end
object.Activated:Connect(function()
print(object.Name.." was activated")
end)
end
for _, object in Main2:GetDescendants() do
buttonFunctionality(object)
end
Main2.DescendantAdded:Connect(buttonFunctionality)
--[[ If any new objects are added to the second "Main" folder, whether it's
directly in the folder or inside of any of the frames, it'll call the
"buttonFunctionality" function to make sure that any buttons that hadn't
existed when the LocalScript first looped through its descendants will have the
button activation event connected to a function --]]