How do I make multiple clickdetector open a GUI with only one function?

Can you try this and see if Step1 and Step2 both print

local model = game.Workspace.Folder

function onMouseClick()
	print("You clicked me!")
end

for i,v in pairs(Folder:GetDescendants()) do
   if v:IsA("ClickDetector") then
      print("Step1")
      v.MouseClick:connect(onMouseClick)
   end
end)

I just noticed that if you’re looping through all descendants, just check if “v” is a ClickDetector instead of checking if it is a BasePart, then checking if it has a ClickDetector.

for i,v in pairs(model:GetDescendants()) do
	if v:IsA("ClickDetector") then
		clickDetector.MouseClick:connect(onMouseClick)
	end
end)

Ha… lol I just followed your updated code and this slipped right past me. Would be v.MouseClick… though

1 Like


Like this?

Just change ClickDetector to v. The underlined one, not in quotes.

1 Like

And so that went right under my nose too, lol.

1 Like

Sadly it did not print

In that case the path might be wrong. Could you send a picture of your workspace (showing the top of workspace) and the open the folder along with other nested contents contents.

image

Could also try, and seeing what all prints.

local model = game.Workspace.Folder

function onMouseClick()
	print("You clicked me!")
end

for i,v in pairs(Folder:GetDescendants()) do
   print(v.Name)
   if v:IsA("ClickDetector") then
      print("Step1")
      v.MouseClick:connect(onMouseClick)
   end
end)

It did not work

Click the Server option? I bet that if it fires on the Server, it wont show up in the client output.

Can you zoom out and show more of your workspace?

image

Is the code contained within that highlighted local script? If so try changing it to a normal script and just copying everything over.

it worked!

image

1 Like

Oof… nice xd. Well gl, glad I could help.

1 Like