MouseButton1Down not working?

I am trying to make a button fire a remote to the server when pressed; however, the MouseButton1Down event does not seem to be working. I have adjusted the ZIndex of the button (to make it clickable) and attempted to print inside of the event, but nothing is printing.
The loop is working, but it is not connecting the function to the event:

for _, frame in ipairs(script:GetDescendants()) do
	if frame.Name == 'Locked' and frame:IsA('Frame') then
		updateLockedFrame(frame)
		frame.Main.Research.Button.MouseButton1Down:Connect(function()
			game:GetService('ReplicatedStorage').Events.Research:FireServer(frame.Type.Value)
		end)
		frame.Main.ResearchMAX.Button.MouseButton1Down:Connect(function()
			game:GetService('ReplicatedStorage').Events.Research:FireServer(frame.Type.Value, true)
		end)
	end
end
2 Likes

Try exchanging ipairs for pairs

I switched it and still doesn’t work.

Exchange GetDescendants() for GetChildren()

Can you show us your explorer?

image image

did you tried putting a print() function when you click the button?

Ensure you are calling that on an object that is higher up then any other elements just to be safe, and that you’re doing it on the frame or button and not the folder.

I have had issues with hit detection, if you have a decorated button just make an element that is invisible and/or higher up on the z index.

You may have to wait for the script’s descendants to load before you loop through them. I think when your script runs, the script’s descendants haven’t loaded in yet and the loop therefore isn’t looping through any descendants

The button is above every other element using ZIndex. image

That makes sense. How would I wait for the descendants to load in?

You can make separate variables for each frame, for example

local Frame1 = script:WaitForChild("Frame1")
local Frame2 = script:WaitForChild("Frame2")

Another way you can do this is to have a table with the name of all the frames you want to loop through, for example

local FRAMES_TO_LOOP = {
 "Frame1";
 "Frame2";
}

for _, frameName in pairs(FRAMES_TO_LOOP) do
 local Frame = script:WaitForChild(frameName)
   --execute code here
end

However, I noticed you want to loop through all descendants of a script that is a frame. In that case, one possible solution is to use a nested table that mimics the organization of these frames in the explorer, like so:

local FRAME_STRUCTURE = {
 Folder1 = {
   "Frame1";
 };
 Folder2 = {
   "Frame2";
 };

}

and you can loop through each nested table and run your looped code on each frame (of course, after you wait for the folders and frames to load)

2 Likes

To keep simplicity, I ended up rewriting the system to make the functions connect using :ChildAdded() after they are cloned to a specific frame and now works.
I appreciate everyone who has helped in the thread :slightly_smiling_face: