What do you want to achieve?
Use a for loop for button formatting even when new buttons are added.
What is the issue?
I can get it to work with the buttons added before the game is started. However, when I try and add buttons via a script after the game has started they don’t work.
What solutions have you tried so far?
I’ve tried using .ChildAdded and then running it again, but I really have no idea what I’m doing.
What I mean is whenever this button is clicked or hovered I can format multiple at once by doing something like:
for _, Button in pairs(script.Parent.Frame:GetChildren()) do
if Button:IsA('TextButton') then
Button.MouseEnter:Connect(function()
Button.BackgroundColor3 = Color3.fromRGB(213, 213, 213)
end)
Button.MouseEnter:Connect(function()
Button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
end)
end
end
Here is the problem though, it takes in a table script.Parent.Frame:GetChildren(), then if anymore are added to that I cannot format them because it has, from what I’ve seen ‘stored’ the original table.
Yeah, you should consider looking into the ChildAdded event. I would create a function for the children being added and hook that up with the ChildAdded Event. Essentially, when a child is added, we’re going to call a function to check whether the child is a textbutton, etc. Since we need to loop over the all of the children, we can simply use a for loop and pass the value (the children) in the function.
local frame = script.Parent.Frame -- location to frame
function NewButton(Button) -- Button is the 'value' being passed in the function
if Button:IsA("TextButton") then
Button.MouseEnter:Connect(function()
Button.BackgroundColor3 = Color3.fromRGB(213, 213, 213)
Button.MouseEnter:Connect(function()
Button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
end)
end)
end
end
for _, v in pairs(frame:GetChildren()) do -- Looping over all the frames children
NewButton(v) -- passing the 'value' in the function
end
frame.ChildAdded:Connect(NewButton) -- Connect ChildAdded to function