Scripting gui buttons that arent there yet

Hey so this is the first time i bump into this scripting gui buttons. Im basically making a list of saved songs on a scrolling gui to load delete or save.

However if there are no songs yet, then theres no button to listen for. So I want to wait for a child button to he added, then trigger multiple event listeners depending on how many buttons are added.

So just to help ppl understand better what im doing, i have a text button in replicated storage. When a player saves a song, the button clones into the scrolling frame and get renamed the name of the song, which gets defined by a module script containing song data.

Now I know this code wont work. But just to give a basic idea of the type of workability im trying to achieve.

local songButtons = scrollingFrame.Frame:GetChildren() -- which may not exist

for i, songButton in ipairs(songButtons) do
     songButton[i].Activated:Connect(function()
          -- code here to rename button
    end)
end

There may be some methods to achieve this that im completely aware of. Any input helps. Thanks

3 Likes

scrollingFrame.Frame.ChildAdded:Connect()
might work for you.

You can do the following:

local Frame = scrollingFrame.Frame
local songButtons = Frame:GetChildren() -- which may not exist

if #songButtons > 0 then
    for i, songButton in ipairs(songButtons) do
        songButton.Activated:Connect(function()
             -- code here to rename button
        end)
    end
end

Frame.ChildAdded:Connect(function(child)
    child.Activated:Connect(function()
         -- code here to rename button
    end)
end)
1 Like

Ok, i think that may work. But i left out one main issue sorry.

So first i have a save mode button that brings you to tge save load screen. I already have a listener for the first button. Then i want it to trigger what was last responded. Tge problem is then id have listeners within a listener and if im not wrong that seems impropper.

What do you mean by listeners?

Sorry. Event listeners. So in my local script. I have a saveMode.Activated:connect(function() which is the listener, then it fires the server with a player parameter.

Next theres a remote event listener in the server that triggers a function where i pull in all the song data from a module script then it fires the client with that data.

Then in the local script I listen for the remote from the server with all the data as parameters.

remote.OnClientEvent:Connect(function(params)
-- in here the code we were talking about. 
end) -- this is when the screen is open.

Since that code is in this listener, itll just fly through the function and it just skips any listeners within it. I tried a while loop here and it yields tge rest of my script, and im waiting for a song button to be selected then another 3 buttons after may be pressed. Save load delete. Sorry, and hope im not being too confusing.

maybe you can use promises? (modify objects that don’t exist yet)

I read up a bit about promises. And sounds like it might provide a solution. Ill have to do more research to implement it but ill do just that and let you kniw how it goes.