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

I’ve looked up a lot of things on the topic, but none of them appear to work. I don’t get it, and I’m still a newbie at lua.

So I have 3 parts here named clicker and I want to make it work with only one function.

I really need help :smiley:

1 Like

Well the most basic method would be as follows:

local clickDetector1 = workspace.Part1.ClickDetector
local clickDetector2 = workspace.Part1.ClickDetector
local clickDetector3 = workspace.Part1.ClickDetector
 
function onMouseClick()
	print("You clicked me!")
end
 
clickDetector1.MouseClick:connect(onMouseClick)
clickDetector2.MouseClick:connect(onMouseClick)
clickDetector3.MouseClick:connect(onMouseClick)

A slightly cleaner and better approach would be to simply loop through the elements. Lets say you have all the parts within a model in the workspace, you could do as follows:

local model = game.Workspace.Model

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

for i,v in pairs(model:GetChildren()) do
   local clickDetector = v:FindFirstChild("ClickDetector")
   if clickDetector then
      clickDetector.MouseClick:connect(onMouseClick)
   end
end)

With this approach you could keep adding the parts into the model and their click cetectors would automatically be linked to the onMouseClick function, without having to change any code.

3 Likes

Can I put it in a folder with multiple parts that has a clickdetector?

A folder works fine, just make sure to change the variable path accordingly.

Thank you so much! Works perfectly Fine!

By the way, I added the parts into the model but the script stopped working
The clickdetectors are insidee the model with the part.
image

You can try using GetDescendants instead of GetChildren, and check if “v” is a BasePart.

model:GetDescendants()
-- Inside the for loop...

if v:IsA("BasePart") then
	local clickDetector = v:FindFirstChild("ClickDetector")
	if clickDetector then
		clickDetector.MouseClick:connect(onMouseClick)
	end
end

for i,v in pairs(model:GetChildren()) do
if v:IsA(“BasePart”) then
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end)

like this?

Change model:GetChildren() to model:GetDescendants()


for i,v in pairs(model:GetDescendants()) do
if v:IsA(“BasePart”) then
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end)

Now, try testing your code and check if there are still problems.

It has red lines

If the path to the click detector is always the same no matter the model then I would just update the path in the loop
Ex.

local clickDetector = v.Part:FindFirstChild("ClickDetector")

Otherwise, as @Y_VRN mentioned, you could just change

:GetChildren()

to

:GetDescendants()

The red is because those arent actual quotes. Maybe you copied from the above and it changed the format?
image

2 Likes

It won’t work still.

Sorry im very dumb when it comes to scripting

Have you changed the names of the ClickDetectors? Try using FindFirstChildOfClass(ClassName) instead.

image

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)