How to use disconnect

So i’ve found a bug in my game that when we click multiples times on the button, and we click use after it, it use the potion as many time u clicked.


and I’ve got an idea, i’ll use :Once() but I got an other problem with :Once()

and after that I got an other idea that I can use :Disconnect() but I have no idea how to use it.

here’s my script when I click on the button and call the function:

ReplicatedStorage.Remotes.BoostData.OnClientEvent:Connect(function(boostNames)
	for i, v in pairs(boostNames) do
		if v == 0 then continue end
		local BoostInfo = nil
		for _, BoostData in ipairs(BoostsData.Boosts) do
			if BoostData.Name == i then
				BoostInfo = BoostData
				break
			end
		end

		if BoostInfo then
			local clonedBoost = TemplateBoost:Clone()
			clonedBoost.Parent = ScrollingFrameBoost
			clonedBoost.Visible = true
			clonedBoost.Name = i
			clonedBoost.Label.Text = ""
			clonedBoost.ImageLabel.Image = BoostInfo.ImageId
			clonedBoost.Label.TextColor3 = BoostInfo.TextColor
			clonedBoost.Label.Font = BoostInfo.TextFont
			clonedBoost.LayoutOrder = BoostInfo.LayoutOder
			clonedBoost.Effect.ImageColor3 = BoostInfo.TextColor

			clonedBoost.MouseButton1Click:Connect(function()
				local BoostInfoData = {
					name = i,
					Multiplier = BoostInfo.Multiplier,
					textColor = BoostInfo.TextColor,
					textFont = BoostInfo.TextFont,
					image = BoostInfo.ImageId
				}
				lastClickedBoost = clonedBoost
				OnBoostClicked(BoostInfoData, clonedBoost)
			end)
			table.insert(currentBoostValue, clonedBoost)
			updateMaxBoostText()
			updateBoostAmount(clonedBoost)
		else
			warn("Boost information not found for " .. i)
		end
	end
end)

and here is the function

local function OnBoostClicked(BoostInfo, clickedButton)
	InfoFrame.AuraFrame.BoostImage.Image = BoostInfo.image
	InfoFrame.BoostName.Text = BoostInfo.name
	InfoFrame.BoostName.TextColor3 = BoostInfo.textColor
	InfoFrame.BoostName.Font = BoostInfo.textFont
	InfoFrame.rng.Text = "X" .. tostring(BoostInfo.Multiplier)
	InfoFrame.Luck.Text = ""
	InfoFrame.Speed.Text = ""
	
	InfoFrame.EquipButton.MouseButton1Click:Once(function()
		InfoFrame.EquipButton.Interactable = false

		if clickedButton == lastClickedBoost then
			if lastClickedBoost == nil then return end
			BoostCount -= 1
			giveBoost(BoostInfo.name)
			updateBoostAmount(clickedButton)
			updateMaxBoostText()
			OnBoostClicked(BoostInfo, clickedButton)
			clickedButton.Active = true
			clickedButton.Interactable = true
			local ValBoost = player.Boosts:FindFirstChild(clickedButton.Name)
			if ValBoost.Value <= 0 then
				clickedButton:Destroy()
				lastClickedBoost = nil
				clickedButton = nil
				InfoFrame.AuraFrame.BoostImage.Image = ""
				InfoFrame.AuraFrame.auraNameText.Text = ""
				InfoFrame.BoostName.Text = ""
				InfoFrame.rng.Text = "X"
				task.wait(0.1)
				InfoFrame.EquipButton.Interactable = true
			else
				task.wait(0.1)
				InfoFrame.EquipButton.Interactable = true
				lastClickedBoost = nil
				clickedButton = nil
			end
		end 
	end)
end

If you guys can help me i’ll really appreciate it, ty :slight_smile:

The main use if :Disconnect() is to terminate a script connection so that it will no longer be listening for the event it’s connected to. For example:

local connection = game.Player.PlayerAdded:Connect(function() end)
connection:Disconnect()

After running :Disconnect(), the function will no longer be fired when the event is called. This is typically used when a connection or an instance connected to it is not currently needed.

:Once() is similar in that it disconnects the connection, but immediately after being fired for the first time.

Also, I’m having some trouble understanding exactly what your goal is here. Can you walk me through exactly what you want to happen when you press each button? What are you wanting to accomplish, and why are you wanting to disconnect the connection? I’m sorry if I’m just not picking it up very well.

No problem ! ty for helping me, So when I click multiples times the same button and click “use” it use the potion as many time I clicked, but I want that the potion use only one time, so i wanted to use Disconnect() because a friend told me to but I have no idea how it works.

is it clear now ? Sorry my english is bad mb if im not clear

Just check if the potion is selected or not?
Like try when the thing is clicked a boolean is set to true and when its unselected just set the boolean to false

I don’t understand what ur saying, I already have something to know if the potion is selected just when I click multiples times the button it call multiples times the function, and when I click the button “use” it use the potion as many time I clicked the button

Thats what im saying, just see if the potion is selected, and if it is selected, just return out of the function

Ok, `while wait(.1) do

updateMaxBoostText()
updateMaxAuraText()

player.BoostCount.Value = BoostCount
player.AuraCount.Value = auraCount

if lastClickedBoost ~= nil then
	IsBoostSelected = true
	OnBoostClicked(BoostInfoData, lastClickedBoost)
else
	IsBoostSelected = false
end

end`

Here the function is called multiples times too, so how can I know when the value changed cause its not an intvalue or else, its just a boolean value in the script so, can we use Changed:Connect() ?

Whats the function that selects a potion? Locate it, find the line where it will select it, add:

If IsBoostSelected then
return
else
--more code idk
end

I hope this helps

that’s not a function that’s just when we click the button, the value of local lastClickedBoost = nil
change to the name of the potion selected

Wait I’ve got an idea

Are you still not getting what im trying to say?

No I understand but I cant call the function when the value changed because I check the value in the while loop,

local function OnBoostClicked(BoostInfo, clickedButton)
	if IsBoostSelected then
		InfoFrame.AuraFrame.BoostImage.Image = BoostInfo.image
		InfoFrame.BoostName.Text = BoostInfo.name
		InfoFrame.BoostName.TextColor3 = BoostInfo.textColor
		InfoFrame.BoostName.Font = BoostInfo.textFont
		InfoFrame.rng.Text = "X" .. tostring(BoostInfo.Multiplier)
		InfoFrame.Luck.Text = ""
		InfoFrame.Speed.Text = ""

		InfoFrame.EquipButton.MouseButton1Click:Once(function()
			InfoFrame.EquipButton.Interactable = false

			if clickedButton == lastClickedBoost then
				if lastClickedBoost == nil then return end
				BoostCount -= 1
				giveBoost(BoostInfo.name)
				updateBoostAmount(clickedButton)
				updateMaxBoostText()
				OnBoostClicked(BoostInfo, clickedButton)
				clickedButton.Active = true
				clickedButton.Interactable = true
				local ValBoost = player.Boosts:FindFirstChild(clickedButton.Name)
				if ValBoost.Value <= 0 then
					clickedButton:Destroy()
					lastClickedBoost = nil
					clickedButton = nil
					InfoFrame.AuraFrame.BoostImage.Image = ""
					InfoFrame.AuraFrame.auraNameText.Text = ""
					InfoFrame.BoostName.Text = ""
					InfoFrame.rng.Text = "X"
					task.wait(0.1)
					InfoFrame.EquipButton.Interactable = true
				else
					task.wait(0.1)
					InfoFrame.EquipButton.Interactable = true
					lastClickedBoost = nil
					clickedButton = nil
				end
			end 
		end)
	else
		return
	end
end

I put the if IsBoostSelected at the start but the function is not called