How to connect multiple ClickDetectors to a single function?

Hello, I know this is probably a really dumb question but I really need help, I’ve been “fighting” with this problem for the past 2 days and it seems like I can’t find a solution. Long short story, I’d like to figure out how to connect a single function to multiple ClickDetectors.

To be exact: I have a function connected to a ClickDetector inside a Part. Once the ClickDetector is clicked, the function is supposed to tween a GUI, opening it.
The script I originally wrote works perfectly if there is only one ClickDetector in the Workspace, it does what it is supposed to do. However, the moment I try to duplicate the part with the ClickDetector inside, the function will be activated only by one of the copies, while the rest will do nothing.

Here is the first version of the script, a simple LocalScript inside the GUI itself:

local plr =  game.Players.LocalPlayer
local PlayerGUI = plr:WaitForChild("PlayerGui")

local Open = game.Workspace.Mug.ClickDetector
local Close = script.Parent.ImageLabel.Close
local Catalog = PlayerGUI.Makeup_GUI.ImageLabel


Open.MouseClick:Connect(function()
	Catalog:TweenPosition(UDim2.new(0.33, 0, 0.357, 0), "Out", "Back", 1, false)
end)

Close.MouseButton1Click:Connect(function()
	Catalog:TweenPosition(UDim2.new(0.33, 0, -0.7, 0), "In", "Back", 1, false)
end)

As I said before, this script works perfectly if there is only one ClickDetector, but won’t work the moment I duplicate it. It seems like that no matter the number of copies, only one part with trigger the function.

I’ve tried a slightly different version of the same script, using loops instead (same LocalScript, always inside the GUI itself), which produced the same result:

local plr =  game.Players.LocalPlayer
local PlayerGUI = plr:WaitForChild("PlayerGui")

local Open = game.Workspace.MakeUpClickDetectors:GetDescendants()
local Close = script.Parent.ImageLabel.Close
local Catalog = PlayerGUI.Makeup_GUI.ImageLabel

for index, descendant in pairs(Open) do
	if descendant:IsA("ClickDetector") then
		Open.MouseClick:Connect(function()
			Catalog:TweenPosition(UDim2.new(0.33, 0, 0.357, 0), "Out", "Back", 1, false)
		end)
	end
end

Close.MouseButton1Click:Connect(function()
	Catalog:TweenPosition(UDim2.new(0.33, 0, -0.9, 0), "In", "Back", 1, false)
end)

I’ve asked around and, while I couldn’t find a proper solution on other platforms, someone suggested to use RemoteEvents, a solution that ultimately produced the same result as the previous versions of the script aka only one of the ClickDetectors will activate the function, while the others will do nothing.

Here are the scripts:
1. Script inside the part itself, with the ClickDetector:

local Players = game:GetService("Players")
local Mirrors = game.Workspace.MakeUpClickDetectors:FindFirstChildOfClass("Part")
local Detectors = Mirrors:WaitForChild("ClickDetector")


local OpenMakeupGui = Instance.new("RemoteEvent")
OpenMakeupGui.Parent = game.ReplicatedStorage
OpenMakeupGui.Name = "OpenMakeupGui"

local function OpenMKUp(player)
	OpenMakeupGui:FireClient(player)
end

Detectors.MouseClick:Connect(OpenMKUp)

2. Script inside ServerScriptService:

local Players = game:GetService("Players")
local Mirrors = game.Workspace.MakeUpClickDetectors:FindFirstChildOfClass("Part")
local Detectors = Mirrors:WaitForChild("ClickDetector")


local OpenMakeupGui = Instance.new("RemoteEvent")
OpenMakeupGui.Parent = game.ReplicatedStorage
OpenMakeupGui.Name = "OpenMakeupGui"

local function OpenMKUp(player)
    OpenMakeupGui:FireClient(player)
end

Detectors.MouseClick:Connect(OpenMKUp)

3. LocalScript inside the GUI:

local plr =  game.Players.LocalPlayer
local PlayerGUI = plr:WaitForChild("PlayerGui")
local Close = script.Parent.ImageLabel.Close
local Catalog = PlayerGUI.Makeup_GUI.ImageLabel

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenMakeupGui = ReplicatedStorage:WaitForChild("OpenMakeupGui")

local Open = game.Workspace.MakeUpClickDetectors:GetChildren()

local function showMakeupFired()
	Catalog:TweenPosition(UDim2.new(0.33, 0, 0.357, 0), "Out", "Back", 1, false)
end
OpenMakeupGui.OnClientEvent:Connect(showMakeupFired)

Close.MouseButton1Click:Connect(function()
	Catalog:TweenPosition(UDim2.new(0.33, 0, -0.9, 0), "In", "Back", 1, false)
end)

None of these attempts displayed errors.

5 Likes

You could just make a tag with collection service and add it to all click detectors, then you iterate through collectionService:GetTagged(thetagyougave) and do this:

for i,v in pairs(collectionService:GetTagged("thetagyougave")) do
      v.ClickDetector:Connect()
end
4 Likes

In the 2nd script you sent, you just set an event for your Open button every time you check if the descendent is a clickdetector.

If you changed the 3rd line of that snippet to descendant.MouseClick, it should work fine, no?

1 Like

Hey sorry for taking so long to answer!
I’m new to LUA, so I had no idea CollectionService was a thing, thank you so much!

I tried your method and this is what I got, using the Tag Editor plug-in:

local plr =  game.Players.LocalPlayer
local PlayerGUI = plr:WaitForChild("PlayerGui")
local CollectionService = game:GetService("CollectionService")

local Open = game.Workspace.MakeUpClickDetectors.Mirror
local Close = script.Parent.ImageLabel.Close
local Catalog = PlayerGUI.Makeup_GUI.ImageLabel


for i,v in pairs(CollectionService:GetTagged("Mirror")) do
    v.ClickDetector:Connect(function()
        Catalog:TweenPosition(UDim2.new(0.33, 0, 0.357, 0), "Out", "Back", 1, false)
    end)
end
    
Close.MouseButton1Click:Connect(function()
    Catalog:TweenPosition(UDim2.new(0.33, 0, -0.7, 0), "In", "Back", 1, false)
end)

Sadly it doesn’t work either, it gives me this error in the output window and I have no idea how to fix it:
image

Oof my bad, I must’ve messed up the scripts lol. It produces the same result it seems… by the way, I’m trying to use @AstralBlu_e’s idea at the moment. :slight_smile:

Make sure you didn’t add the tag to the local script instead of only the mirrors. Because thats what is happening

And I noticed you were doing v.ClickDetector:Connect(function() when it shouldve been v.ClickDetector.MouseClick:Connect(function()

If the problem persists just add this before the event.

if v:FindFirstChild("ClickDetector") then
      -- The event
end
2 Likes

Oh, LMAO it was exactly that, took me 10 minutes to figure out how to remove the tag via plug-in… works perfectly now!

Yeah, I fixed it in Studio right after I posted the last answer. Thank you so much for your help!!

1 Like