i have a function that opens like details for a specific item,
and i call the function everytime the player clicks a button and inside the function i have another event for mousebuttonclick and somehow everytime the player clicks the button it duplicate the script
soo first times prints it once
second 2 times
etc.
if there is a way to stop an event from continuing to happen please let me know
(i tried return did not work)
sorry if its a bit messy
btw this function runs everytime the player clicks an item
local function Open(child,status: "fast" | "slow")
local tween = tweenservice:Create(frame,TweenInfo.new(TweenTime,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{Position = Positions.framePositionNew})
ItemSelected = child.Name
Item.ItemBio.Text = bios[ItemSelected]
Item.ItemName.Text = ItemSelected
Item.ItemType.Text = "Item Type: "..types[ItemSelected]
if types[ItemSelected] == "Equipible" then
Item["Un/Equip"].AutoButtonColor = true
if player.Backpack:FindFirstChild(ItemSelected) then -- if the item is found
Item["Un/Equip"].Text = "UnEquip"
statusItem("Red")
else
Item["Un/Equip"].Text = "Equip"
statusItem("Green")
end
Item["Un/Equip"].MouseButton1Click:Connect(function() -- HERE
print(ItemSelected)
if player.Backpack:FindFirstChild(ItemSelected) then
player.Backpack:FindFirstChild(ItemSelected):Destroy()
Close(child)
else
rep.Tools:FindFirstChild(ItemSelected):Clone().Parent = player.Backpack
Close(child)
end
end)
else
Item["Un/Equip"].Text = "UnEquipible"
Item["Un/Equip"].AutoButtonColor = false
statusItem("Red")
end
opened = true
if status == "fast" then
StopViewPortFrame()
if frame.Visible == true then
rep.Models:FindFirstChild(ItemSelected).PrimaryPart:Clone().Parent = Item:FindFirstChild("ViewPortFrame")
end
Item.Visible = true
else
cd = true
tween:Play()
task.delay(TweenTime,function()
if ItemSelected ~= nil and opened == true then
StopViewPortFrame()
if frame.Visible == true then
rep.Models:FindFirstChild(ItemSelected).PrimaryPart:Clone().Parent = Item:FindFirstChild("ViewPortFrame")
end
Item.Visible = true
task.wait(0.2)
cd = false
end
end)
end
end
It looks like this is happening because you’re repeatedly connecting a function to the MouseButton1Click event and never disconnecting it so they just accumulate every time you open the UI. If you’re going to be adding connections when opening up the UI, you need to disconnect them when the UI closes.
Ah, if you’re going to disconnect it in the callback function itself, it’d be better to just use MouseButton1Click:Once(...). That way you don’t need to worry about the Connection variable at all.