Function fires once then fires twice then thrice and so on

Hello there! I have a problem with functions and I don’t know how to solve it. As the title says the function bellow fires once then keeps multiplying forever:

local function clickDetectorHandler(clickDetector, highlight)
	local box = clickDetector.Parent
	local boxHealth = box:FindFirstChild("Health")
	local boxMaxHealth = box:FindFirstChild("MaxHealth")
	local healthIndicator = box:FindFirstChild("HealthIndicator")

	clickDetector.MouseHoverEnter:Connect(function()
		highlight.Enabled = true
	end)

	clickDetector.MouseHoverLeave:Connect(function()
		highlight.Enabled = false
	end)

	clickDetector.MouseClick:Connect(function()
		local randomSound = Sounds.BoxHit:GetChildren()
		local random = math.random(1, #randomSound)
		local chosenSound = randomSound[random]
		
		healthIndicator.MainFrame.HealthIndicator.BoxHealth.Text = boxHealth.Value .. "/" .. boxMaxHealth.Value
		healthIndicator.MainFrame.BoxName.Text = box.Name

		chosenSound:Play()
		
		if boxHealth.Value > 0 then
			BoxRemotes.BoxDamaged:InvokeServer(box)
		elseif boxHealth.Value <= 0 then
			BoxRemotes.BoxOpened:FireServer(box)
			
			if #Inventory:GetChildren() <= maxInvSpace.Value then -- problem is here
				Inventory.DescendantAdded:Connect(function(child)
					print(child)
				end)
			end
			
			wait(.1)
			
			local clone = RewardFrame:Clone()
			clone.Parent = ui
			local itemImage = clone.ItemImage
			local sparkles = clone.Sparkles
			local itemName = clone.ItemName
			local rarity = clone.Rarity
			
			for i, item in pairs(Inventory:GetChildren()) do
				local itemName_ = item.Value
				itemName.Text = itemName_
				if item:IsA("StringValue") then
					for i ,v in pairs(Items:GetDescendants()) do
						if v.Name == itemName_ then
							itemImage.Image = v.Value
						end
					end
				end
			end
			
			clone:TweenSize(UDim2.new(0,471,0,471), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .5, true)
			itemImage:TweenSize(UDim2.new(0,471,0,471), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .5, true)
			sparkles:TweenSize(UDim2.new(0,471,0,471), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .5, true)
			itemName:TweenSize(UDim2.new(0, 256,0, 50), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .5, true)
			rarity:TweenSize(UDim2.new(0, 151,0, 26), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .5, true)
			Sounds.ItemReward:Play()
			
			for i=1, 100 do
				wait(.0001)
				sparkles.Rotation += 1
			end
			
			clone:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .2, true)
			itemImage:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .2, true)
			sparkles:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .2, true)
			itemName:TweenSize(UDim2.new(0, 0,0, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .2, true)
			rarity:TweenSize(UDim2.new(0, 0,0, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .2, true)
			
			wait(1.5)
			clone:Destroy()
		end
	end)
end
local function boxHandler()
	for i, v in pairs(Boxes:GetDescendants()) do
		if v:IsA("UnionOperation") then
			local clickDetector = Instance.new("ClickDetector")
			clickDetector.Parent = v

			local highlight = Instance.new("Highlight")
			highlight.Parent = v
			highlight.Enabled = false	
			highlight.FillTransparency = 1
			
			local clone = healthIndicator:Clone()
			clone.Parent = v
			clone.Adornee = v
			clone.MainFrame.HealthIndicator.BoxHealth.Text = v.Health.Value .. "/" .. v.MaxHealth.Value
			clone.MainFrame.BoxName.Text = v.Name

			clickDetectorHandler(clickDetector, highlight) -- here I call the function
			print(clickDetector, highlight)
		end
	end
end

Happens because you connect the Inventory.DescendantAdded event whenever the click detector is clicked, without disconnecting the last one. This means that everytime the click detector is activated, it’ll connect the same function to that event once again, making it run twice, thrice, four times, etc…

You should move that connection OUTSIDE the clickDetector.MouseClick callback

1 Like

(you should move this to #help-and-feedback:scripting-support )

Like @hashyyyyeryjrjej said, you have connections inside of connections and they all build up. You can use the method they suggested, but also if you have variables contained within that subprogram you need in those connections, you can consider using :Once or using :Disconnect on the connections you only need once/don’t need anymore.

it worked! thank you very much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.