How would i get this to work

a couple of things:

1st: how would i make this work?

workspace.PieceA.ClickDetector.MouseClick:Connect(function(hit)
	local PieceA = workspace.PieceA
	
end)


workspace.PieceB.ClickDetector.MouseClick:Connect(function(hit)
	local PieceB = workspace.PieceB
end)


workspace.PieceC.ClickDetector.MouseClick:Connect(function(hit)
	local PieceC = workspace.PieceC
end)


workspace.PieceD.ClickDetector.MouseClick:Connect(function(hit)
	local PieceD = workspace.PieceD
end)


workspace.PieceE.ClickDetector.MouseClick:Connect(function(hit)
	local PieceE = workspace.PieceE
end)


workspace.PieceF.ClickDetector.MouseClick:Connect(function(hit)
	local PieceF = workspace.PieceF
end)


workspace.PieceG.ClickDetector.MouseClick:Connect(function(hit)
	local PieceG = workspace.PieceG
end)


workspace.PieceH.ClickDetector.MouseClick:Connect(function(hit)
	local PieceH = workspace.PieceH
end)


workspace.PieceI.ClickDetector.MouseClick:Connect(function(hit)
	local PieceI = workspace.PieceI
end)

i want to make it so each of them can only be clicked once which can be done with a debounce. Would it be best to just add like

debounceA = false
debounceB = false
debounceC = false
debounceD = false
...

or is there another way?

2: if all these functions are going to do different stuff, is there a better way than just putting a lot of "part.ClickDetector.MouseClick:Connect(function(hit)

1 Like

Having variables to track the state like this is really common and works fine for simple situations, but there are other ways. I usually prefer disconnecting connections when they’re no longer needed. That way there’s fewer globally accessible state variables that other parts of the code can interact with and end up breaking. Something like this:

function setupCollectible(collectible: Part)
    local clickConnection = setupSingleClickDetection(collectible.ClickDetector, function()
        onCollectibleClicked(collectible)
    end)
end

function setupSingleClickDetection(cd: ClickDetector, callback): RBXScriptConnection
    local c
    c = cd.MouseClick:Connect(function(...)
        c:Disconnect() --This is what makes it "single click".
        callback(...)
    end)
    return c
end

function onCollectibleClicked(collectible: Part)
    print("Got " .. collectible:GetFullName())
end

for _, part in ipairs(workspace.PartsFolder:GetChildren()) do
	setupCollectible(part)
end

Alright, Thanks!
(Post must be 30 letters)