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