Help regarding MouseButton1Down [One question left]

Hi, so I made a system where I have some TextButtons and there is a function which clones it inside SurfaceGui. Now I want to get which player clicked it, so I decided to use LocalScript which ended up like this

local scrollingframe= workspace.Part.SurfaceGui.ScrollingFrame

scrollingframe.ChildAdded:Connect(function(child) --whenever something gets inside it
if child:IsA("TextButton") then-- if child is a TextButton
child.MouseButton1Down:Connect(function()
--do the code
end)
end
end)

Although it didn’t took much time for me to realize that this would only run when a child is added, that means MouseButton1Down event would only run when a child is added. So I tried adding connections to a table like this

local scrollingframe= workspace.Part.SurfaceGui.ScrollingFrame
local connections = {} --all functions are stored here

scrollingframe.ChildAdded:Connect(function(child) --whenever something gets inside it
if child:IsA("TextButton") then-- if child is a TextButton
connections[#connections + 1] = child.MouseButton1Down:Connect(function() --indexed it
--do the code
end)
end
end)

However, these just worked for me and not anyone playing the game. Is there any alternative way to achieve what I want?
Help is very much respected & appreciated.
Thanks!

1 Like

If the server manages the contents of the SurfaceGui, you might’ve gotten yourself into some adaptability issues. UI is meant to be managed locally, and the problem here is noticeable.

If children are added to the SurfaceGui, this fires ChildAdded, as expected. However, if the children are added before players join, there will not be new connections.

You can convert the anonymous function into a standard one, and loop through existing children:

local scrollingframe = workspace.Part.SurfaceGui.ScrollingFrame

local function listenToInput(child : TextButton?) --whenever something gets inside it
	if child:IsA("TextButton") then-- if child is a TextButton
		child.MouseButton1Down:Connect(function()
			--do the code
		end)
	end
end

for _, item in ipairs(scrollingframe:GetChildren()) do
	-- loop through existing children to listen to their inputs
	listenToInput(item)
end

scrollingframe.ChildAdded:Connect(listenToInput)

But I doubt this will be scalable in the future. Without knowing how you will handle this serverside, this is as much help as I can do.

1 Like

You know what, just before you posted this, I got this idea into my mind. Although I wasn’t very much sure about is this idea good. I know it isn’t much scalable, but I’m confident now and it would pretty much do the trick for me. I almost lost hope about getting any answer, you literally saved me and I got confident about this idea. Thanks again!

1 Like

One quick question though. I have this script but what if loop runs when no TextButton is inside the ScrollingFrame? Would this loop update after child getting added?

Any object added to the ScrollingFrame (or any instance, really) will fire its ChildAdded event.

Extra note

You’ll also have to use ChildRemoved if you want to detect any removal from the ScrollingFrame.

To make sure that any connections are Disconnected (assuming the buttons are relocated/reparented instead of getting Destroy()ed), store the connections in a table, with the Instance as the key, and the RBXScriptConnection as the value. (e.g table[TextButtonObject] = blahblah.MouseButton1Click:Connect(somethingFunc).

When ChildRemoved is triggered, it will return the object, in which you can use to access, then disconnect and nullify the connection in the table.

1 Like

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