Function Running multiple times

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)

1 Like

could you provide the script you’re using?

2 Likes

sorry if its a bit messy :smile:
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
1 Like

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.

1 Like

sorry but i’ve never used disconnect, how do i do it exactly?

1 Like

where did you try return last time?

i tried at the last line and in the event

1 Like

try adding it at the end of the MouseButton1Click Event

1 Like

sadly still the same issue, maybe the disconnect method will work

1 Like

From the documentation: RBXScriptSignal | Documentation - Roblox Creator Hub,
Connect returns a RBXScriptConnection. You can call Disconnect on that object to remove the function from the event callback list.

In your case, you need to save the connection when you connect it like below:

local Connection = Item["Un/Equip"].MouseButton1Click:Connect(function()
	-- ...
end)

Then when the UI closes, you can call Connection:Disconnect()

2 Likes


sorry but its giving me an error
“Unknown global “Connection””

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.

oh wow thank you soo much, never know this event existed, prob gonna use it A LOT
:heart:

1 Like

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