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.