CollectionService isn't detecting with part I clicked

Hello,

I want this script below to work, it works fine. However it doesn’t detect which part I clicked.

I want the script to print the clicked part’s name, I have lucky block 1, 2, 3, etc. but it only prints LuckyBlock1 even though I am clicking on the other ones too.

local CollectionService = game:GetService("CollectionService")
local Tag = "Lucky Block"

local TaggedParts = CollectionService:GetTagged(Tag)

local debounce = false

for _, TaggedPart in ipairs(TaggedParts) do
	local function onClicked(Player)
		print(TaggedPart.Name) --Here
		if debounce == false then
			debounce = true

			local Health = TaggedPart.Health
			local Open = TaggedPart.Open
			
			if Open.Value == false then
				if Health.Value > 0 then

					local Character = Player.Character or Player.CharacterAdded:Wait()
					local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
					local Humanoid = Character:FindFirstChild("Humanoid")
					local shouldLookAt = Vector3.new(TaggedPart.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, TaggedPart.HumanoidRootPart.Position.Z)

					HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)

					local Animation = script.SmashAnimation
					local AnimationTrack = Humanoid:LoadAnimation(Animation)
					AnimationTrack:Play()
					wait(0.75)
					debounce = false
				end	
			end	

			local EffectsFolder = TaggedPart.Effect
			local Effect1 = EffectsFolder.Area.Sparkles
			local Effect2 = EffectsFolder.Core.Attachment.Glow
			local Effect3 = EffectsFolder.Core2.Attachment.Rays1
			local Effect4 = EffectsFolder.Core2.Attachment.Rays2
			local Effect5 = EffectsFolder.Core2.PointLight


			local RS = game:GetService("ReplicatedStorage")
			local Event = RS:FindFirstChild("Health")

			local Health = TaggedPart.Health
			local OpenValue = TaggedPart.Open

			if OpenValue.Value == false then
				if Health.Value > 0 then
					Health.Value -= 50

					Event:FireClient(Player, Health.Value)
					wait(0.5)

					if Health.Value < 50 then
						local Animation = script:WaitForChild("OpenAnimation")
						local AnimationController = TaggedPart:WaitForChild("AnimationController")
						local Open = AnimationController:LoadAnimation(Animation)
						wait(0.25)
						Open:Play()

						Effect1.Enabled = true
						Effect2.Enabled = true
						Effect3.Enabled = true
						Effect4.Enabled = true
						Effect5.Enabled = true

						OpenValue.Value = true
						wait(1)
						TaggedPart.Top:Destroy()	
						wait(0.75)
						debounce = false	
					end
				end
			end	
		end	
	end

	local function onClicked2(Player)

		local InsertService = game:GetService("InsertService")

		local Gears = {
			47620, -- Slingshot                                
			47586, -- Time Bomb                                                    
			246270069, -- Katana                                
			47637, -- Rocket Launcher
		}

		local Health = TaggedPart.Health
		local Open = TaggedPart.Open

		if Open.Value == true then
			if Health.Value < 50 then

				TaggedPart.ClickDetector:Destroy()

				local Character = Player.Character or Player.CharacterAdded:Wait()
				local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
				local shouldLookAt = Vector3.new(TaggedPart.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, TaggedPart.HumanoidRootPart.Position.Z)
				local Humanoid = Character:FindFirstChild("Humanoid")
				local Animation = script.GrabAnimation
				local AnimationTrack = Humanoid:LoadAnimation(Animation)
				HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
				AnimationTrack:Play()		
				wait(0.4)

				local RandomGear = Gears[math.random(1,#Gears)]

				local Backpack = Player:WaitForChild("Backpack")
				local NewGear = InsertService:LoadAsset(RandomGear)

				local Tool = NewGear:FindFirstChildOfClass("Tool")
				if Tool then
					wait(0.5)
					Tool.Parent = Backpack
					wait(0.75)
				end
			end
		end	

		local connections = {}

		local function onInstanceAdded(object)
			if object:IsA("Model") then
				connections[object] = object.ClickDetector.MouseClick:Connect(onClicked)
				connections[object] = object.ClickDetector.MouseClick:Connect(onClicked2)
			end
		end

		local function onInstanceRemoved(object)
			if connections[object] then
				connections[object]:disconnect()
				connections[object] = nil
			end
		end

		CollectionService:GetInstanceAddedSignal(Tag):Connect(onInstanceAdded)

		for _, object in pairs(CollectionService:GetTagged(Tag)) do
			onInstanceAdded(object)
		end
	end
	TaggedPart.ClickDetector.MouseClick:Connect(onClicked)
	TaggedPart.ClickDetector.MouseClick:Connect(onClicked2)
end

Rather than using MouseClick use Activated as it will work with touch enabled devices.
For your question, you can trigger a function to pass the object clicked:

TaggedPart.ClickDetector.MouseClick:Connect(function(player)
	onClicked(player, TaggedPart)
end)

Your onClicked function will then receive player and the TaggedPart as variables.

Where would I put the variable of `onClicked’ and what would it look like?

would turn into:

local function onClicked(Player, part)

For everything I use ‘part’, so that is the part that is being clicked right.

Yes it would be right. The clicked part identified as TaggedPart is sent to the function along with the player

It doesn’t register the click.

local CollectionService = game:GetService("CollectionService")
local Tag = "Lucky Block"

local TaggedParts = CollectionService:GetTagged(Tag)

local debounce = false

for _, TaggedPart in ipairs(TaggedParts) do
	--TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
		--onClicked(Player, TaggedPart)
		local function onClicked(Player, Part)		
		print(Part.Name)
		if debounce == false then
			debounce = true

			local Health = Part.Health
			local Open = Part.Open
			
			if Open.Value == false then
				if Health.Value > 0 then

					local Character = Player.Character or Player.CharacterAdded:Wait()
					local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
					local Humanoid = Character:FindFirstChild("Humanoid")
					local shouldLookAt = Vector3.new(Part.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, Part.HumanoidRootPart.Position.Z)

					HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)

					local Animation = script.SmashAnimation
					local AnimationTrack = Humanoid:LoadAnimation(Animation)
					AnimationTrack:Play()
					wait(0.75)
					debounce = false
				end	
			end	

			local EffectsFolder = Part.Effect
			local Effect1 = EffectsFolder.Area.Sparkles
			local Effect2 = EffectsFolder.Core.Attachment.Glow
			local Effect3 = EffectsFolder.Core2.Attachment.Rays1
			local Effect4 = EffectsFolder.Core2.Attachment.Rays2
			local Effect5 = EffectsFolder.Core2.PointLight


			local RS = game:GetService("ReplicatedStorage")
			local Event = RS:FindFirstChild("Health")

			local Health = Part.Health
			local OpenValue = Part.Open

			if OpenValue.Value == false then
				if Health.Value > 0 then
					Health.Value -= 50

					Event:FireClient(Player, Health.Value)
					wait(0.5)

					if Health.Value < 50 then
						local Animation = script:WaitForChild("OpenAnimation")
						local AnimationController = Part:WaitForChild("AnimationController")
						local Open = AnimationController:LoadAnimation(Animation)
						wait(0.25)
						Open:Play()

						Effect1.Enabled = true
						Effect2.Enabled = true
						Effect3.Enabled = true
						Effect4.Enabled = true
						Effect5.Enabled = true

						OpenValue.Value = true
						wait(1)
						Part.Top:Destroy()	
						wait(0.75)
						debounce = false	
					end
				end
			end	
		end	
	end

	local function onClicked2(Player, Part)

		local InsertService = game:GetService("InsertService")

		local Gears = {
			47620, -- Slingshot                                
			47586, -- Time Bomb                                                    
			246270069, -- Katana                                
			47637, -- Rocket Launcher
		}

		local Health = Part.Health
		local Open = Part.Open

		if Open.Value == true then
			if Health.Value < 50 then

				Part.ClickDetector:Destroy()

				local Character = Player.Character or Player.CharacterAdded:Wait()
				local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
				local shouldLookAt = Vector3.new(Part.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, Part.HumanoidRootPart.Position.Z)
				local Humanoid = Character:FindFirstChild("Humanoid")
				local Animation = script.GrabAnimation
				local AnimationTrack = Humanoid:LoadAnimation(Animation)
				HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
				AnimationTrack:Play()		
				wait(0.4)

				local RandomGear = Gears[math.random(1,#Gears)]

				local Backpack = Player:WaitForChild("Backpack")
				local NewGear = InsertService:LoadAsset(RandomGear)

				local Tool = NewGear:FindFirstChildOfClass("Tool")
				if Tool then
					wait(0.5)
					Tool.Parent = Backpack
					wait(0.75)
				end
			end
		end	

		local connections = {}

		local function onInstanceAdded(object)
			if object:IsA("Model") then
				connections[object] = object.ClickDetector.MouseClick:Connect(onClicked)
				connections[object] = object.ClickDetector.MouseClick:Connect(onClicked2)
			end
		end

		local function onInstanceRemoved(object)
			if connections[object] then
				connections[object]:disconnect()
				connections[object] = nil
			end
		end

		CollectionService:GetInstanceAddedSignal(Tag):Connect(onInstanceAdded)

		for _, object in pairs(CollectionService:GetTagged(Tag)) do
			onInstanceAdded(object)
		end
	end
	TaggedPart.ClickDetector.MouseClick:Connect(onClicked)
	TaggedPart.ClickDetector.MouseClick:Connect(onClicked2)
end

Because you have reverted back to your old style of Connect events. Change each one to:

TaggedPart.ClickDetector.MouseClick:Connect(function(player)
	onClicked(player, TaggedPart)
end)
TaggedPart.ClickDetector.MouseClick:Connect(function(player)
	onClicked2(player, TaggedPart)
end)

Part is being underlined by red.

	TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
		onClicked(Player, Part)
	end)
	TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
		onClicked2(Player, Part)
	end)

Replace Part with TaggedPart

Now I have 2 TaggedPart’s which one do I choose for the variables?

You are not understanding what the code is doing here.

  1. In your original code, you are looping through all Tagged Parts:
    for _, TaggedPart in ipairs(TaggedParts) do
  2. Within that for loop, you use the same references to create the MouseClick:Connect functions which pass the part name through to the functions

I think you are3 fundamentally misunderstanding how CollectionService and functions in general work. You do not need to embed your functions within your for loop. These should be separate, called upon when required, and passed a parameter, such as TaggedPart when called upon.
I recommend learning more about how functions work. The Roblox Education site is a great resource for this:
https://education.roblox.com/en-us/resources/intro-to-coding-coding-2-functions-1-practice
Yes, wee could re-write your code yo get it to work, but that is not going to help you learn the fundamentals.